Reputation:
I have this table contains name and value, how can i convert the contents of
$row=mysql_fetch_array($result);
into checkboxes?
Upvotes: 0
Views: 148
Reputation: 300825
Assuming $row is an associative array mapping column name onto a boolean 1 or 0 value, you could do something like this:
foreach($row as $colname=>$boolean)
{
//create a name for the checkbox which will produce a nice
//PHP array of checked column names in $_POST['col']
$name="col[$colname]";
//create an id for the checkbox
$id='col'.$colname;
//now we output the checkbox - on form submission you will
//see an element in $_POST['col'][$colname] if checked, and
//no element at all if unchecked...
echo '<input type="checkbox" value="1" '.
'name="'.$name.'" id="'.$id.'" '.
($boolean?'checked="checked"':'').
'>';
//output a label - note we'd tied this to the id of the checkbox
//which means you can click the label to tick the box
echo "<label for=\"$id\">colname</label><br/>";
}
When the form is submitted, you'll get an array in $_POST['col'] indexed by column name, but only for those boxes which are checked, so you'd set to false any columns which are not set.
Upvotes: 3
Reputation: 15756
See here: http://dev.w3.org/html5/spec/Overview.html#checkbox-state
So, try this:
<input type="checkbox" checked="true">
Upvotes: 0