根据MySQL数据渲染HTML复选框输入

时间:2009-01-14 09:58:51

标签: php mysql

我有这个表包含名称和值,我该如何转换

的内容
$row=mysql_fetch_array($result);

进入复选框?

2 个答案:

答案 0 :(得分:3)

假设$ row是一个关联数组,将列名映射到布尔值1或0,你可以这样做:

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/>";
}

提交表单时,您将在$ _POST ['col']中获得一个由列名索引的数组,但仅限于那些已检查的框,因此您将任何未设置的列设置为false

答案 1 :(得分:0)

见这里:http://dev.w3.org/html5/spec/Overview.html#checkbox-state

所以,试试这个:

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