0

I have this table contains name and value, how can i convert the contents of

$row=mysql_fetch_array($result);

into checkboxes?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

See here: http://dev.w3.org/html5/spec/Overview.html#checkbox-state

So, try this:

<input type="checkbox" checked="true">

1 Comment

You may have to replace the "true" with your php call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.