...

View Full Version : returning a checkbox array - help with code



jasboy
09-14-2005, 02:14 PM
hi there

i have a mysql db table with a field named 'ans'.
i also have a form with several checkboxes, which when checked implode into a comma delimited string. all this works fine.

my problem occurs when i wish to return the string back to checkboxes for editing. here's what i have so far, but it doesn't work.

it works if i use this line instead, but it needs to pull in from the db


$saved_ans = array('a', 'b');


what am i doing wrong?? please help!




<?php
$ans = array('a', 'b', 'c', 'd');
$saved_ans = explode(',',$ans);

for ($i = 0; $i < count($ans); $i++){
$checked = in_array($ans[$i],$saved_ans)? " checked" : "";
echo "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n";
}
?>



cheers, jas

raf
09-14-2005, 02:42 PM
Welcome here!


...it works if i use this line instead, but it needs to pull in from the db


$saved_ans = array('a', 'b');


what am i doing wrong?? please help!




<?php
$ans = array('a', 'b', 'c', 'd');
$saved_ans = explode(',',$ans);

for ($i = 0; $i < count($ans); $i++){
$checked = in_array($ans[$i],$saved_ans)? " checked" : "";
echo "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n";
}
?>

You don't realy show the most crucial code --> your db-result processing.
This $saved_ans = explode(',',$ans); can never work since you're exploding the array from the line above.

to do this with your db-value, you'll normally just have something like


$sql = "SELECT your_saved_options FROM yourtable WHERE yourid=$whatever";
$result = mysql_query($sql) or die ('Queryproblem');
if (mysql_num_rows($result) === 1){
$ans = array('a', 'b', 'c', 'd');
$saved_ans = explode(',', mysql_result($result,0));

for ($i = 0; $i < count($ans); $i++){
$checked = in_array($ans[$i],$saved_ans)? " checked" : "";
echo "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n";
}
}

jasboy
09-14-2005, 02:55 PM
Sorry about missing stuff. did you mean this?



$colname_Recordset1 = "1";
if (isset($HTTP_GET_VARS['id'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
}
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT * FROM q01 WHERE id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


all help very much appreciated!
jas

jasboy
09-14-2005, 03:00 PM
also, here is the complete form:


<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td width="27" align="right" nowrap>Answers:</td>
<td width="270">
<?php
$ans = array('a', 'b', 'c', 'd');
$saved_ans = explode(',',$ans);

for ($i = 0; $i < count($ans); $i++){
$checked = in_array($ans[$i],$saved_ans)? " checked" : "";
echo "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n";
}
?>
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Update Record">
</td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1">
<input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>">
</form>


cheers raf.

raf
09-14-2005, 05:06 PM
Sorry about missing stuff. did you mean this?



$colname_Recordset1 = "1";
if (isset($HTTP_GET_VARS['id'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
}
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT * FROM q01 WHERE id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


we need to know the columnname where you store the selected options. in your code, you'd then need to use


$saved_ans = explode(',', ($row_Recordset1['your_column_name']));

where your_column_name then needs to be replaced by the actual columnname.

jasboy
09-14-2005, 05:08 PM
if attempted raf's example but to no avail. i'm a novice with php.
could you simplify it for me??

cheers

jasboy
09-14-2005, 05:12 PM
the column name is "ans". sorry to be a pain!!

jasboy
09-14-2005, 06:19 PM
still not working...

i keep trying. trial and error. lots of errors though!!
this is what i have so far...


<?php
$sql*=*"SELECT*ans*FROM*tester*WHERE ans='{$row_Recordset1['ans']}'";
$result*=*mysql_query($sql)*or*die*('Queryproblem');
if*(mysql_num_rows($result)*===*1){
*****$ans*=*array('a',*'b',*'c',*'d');
*****$saved_ans*=*explode(',',*mysql_result($result,0));

*****for*($i*=*0;*$i*<*count($ans);*$i++){*
********$checked*=*in_array($ans[$i],$saved_ans)?*" checked"*:*"";*
********echo*"$ans[$i]: <input*type=\"checkbox\"*name=\"ans[$i]\"*value=\"$ans[$i]\"$checked><br>\n";*
*****}*
}
?>

jasboy
09-15-2005, 12:58 AM
can anyone else help me on this problem? it seems so close, but it;s just quite not there yet.

cheers guys

raf
09-15-2005, 01:01 AM
what's the value of {$row_Recordset1['ans']} in
$sql="SELECT ans FROM tester WHERE ans='{$row_Recordset1['ans']}'";
?

add and
echo $sql;

right after that $sql="SELECT etc" line and then look at the posted query to see if a value is set for it.

jasboy
09-15-2005, 01:15 AM
i'm very lost on all this now.
would you be able to print out the complete script?

my database table is called "tester"
the table is called "q01"
the "id" is url parameter driven. ie. editPage.php?id=5
the field which i pull the comma separated string from is called "ans"
the recordset is called "Recordset1"

for ease, lets say that the values in the field "ans" are a,b,c,d

also, for ease, lets say there are 6 checkboxes in the form with values of the following: a,b,c,d,e,f

i hope this makes sense. many thanks for your time spent on this!!

raf
09-15-2005, 01:40 AM
$sql = "SELECT ans FROM q01 WHERE id=". $_GET['id'];
$result = mysql_query($sql) or die ('Queryproblem');
if (mysql_num_rows($result) === 1){
$ans = array('a', 'b', 'c', 'd');
$saved_ans = explode(',', mysql_result($result,0));

for ($i = 0; $i < count($ans); $i++){
$checked = in_array($ans[$i],$saved_ans)? ' checked="checked"' : '';
echo $ans[$i], ': <input type="checkbox" name="', ans[$i] ,'" value="', $ans[$i], '" ', $checked, '><br />';
}
}


from your explanation, i can't realy make up what the table is called ... tester or q01 ?

if you get error, then post the first errormessage that you get.

jasboy
09-15-2005, 11:07 AM
sorry, my mistake,

the database is called "tester"
the table is called "q01"
the field that has configurations of 'a,b,c,d,e' is called "ans"

thanks for all your help!

jasboy
09-15-2005, 02:58 PM
this is the entire page. i get this error:
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /Library/Apache2/htdocs/chBoxT/edit.php on line 53



<?php require_once('Connections/conn.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($HTTP_POST_VARS["MM_update"])) && ($HTTP_POST_VARS["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE q01 SET ans=%s WHERE id=%s",
GetSQLValueString(implode(',',$_POST['ans']), "text"),
GetSQLValueString($HTTP_POST_VARS['id'], "int"));

mysql_select_db($database_conn, $conn);
$Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());

$updateGoTo = "s.php";
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

$colname_Recordset1 = "1";
if (isset($HTTP_GET_VARS['id'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
}
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT * FROM q01 WHERE id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td width="27" align="right" nowrap>Ans:</td>
<?php
$sql*=*"SELECT ans FROM q01 WHERE id=".*$_GET['id'];
$result*=*mysql_query($sql)*or*die*('Queryproblem');
if*(mysql_num_rows($result)*===*1){
*****$ans*=*array('a',*'b',*'c',*'d');
*****$saved_ans*=*explode(',',*mysql_result($result,0));

*****for*($i*=*0;*$i*<*count($ans);*$i++){*
********$checked*=*in_array($ans[$i],$saved_ans)?*' checked="checked"'*:*'';*
********echo*$ans[$i],*': <input type="checkbox" name="',*ans[$i]*,'" value="',*$ans[$i],*'" ',*$checked,*'><br />';*
*****}*
}
?>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Update Record">
</td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1">
<input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>">
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


any thought??

jasboy
09-16-2005, 05:58 PM
still not been able to figure out why this isn't working??

i have to go back to my original code as this is the closest i have come to reaching a solution:


<?php*
$ans*=*array('a',*'b',*'c',*'d');
$saved_ans*=*explode(',',$ans);

for*($i*=*0;*$i*<*count($ans);*$i++){*
$checked*=*in_array($ans[$i],$saved_ans)?*" checked"*:*"";*
echo*"$ans[$i]: <input*type=\"checkbox\"*name=\"ans[$i]\"*value=\"$ans[$i]\"$checked><br>\n";*
}*
?>


as i have stated, the database is called 'tester'
the table is called 'q01'
the field i wish to edit is called 'ans'

i don't have a problem populating the field with a comma separated string from a series of checkboxes, but i do have a problem pulling it back to edit via checkboxes.

any thoughts please, most welcome??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum