php从mysql查询中选择输入复选框

时间:2012-04-14 12:21:24

标签: php mysql

如何在checked="yes"时从php mysql结果中添加$row['checkbox'] == input.value

例如:如果现在$row['checkbox']='3'如何在checked="yes"中添加<li><input type="checkbox" name="my_check" value="3" checked="yes" />Guangzhou</li>?感谢。

<?php
// db_connect, mysql_query
while($row = mysql_fetch_array($query)){
// If($row['checkbox']='3)
?>
<li><input type="checkbox" name="my_check" value="1" />Beijing</li>
<li><input type="checkbox" name="my_check" value="2" />Shanghai</li>
<li><input type="checkbox" name="my_check" value="3" />Guangzhou</li>
<li><input type="checkbox" name="my_check" value="4" />Chongqin</li>
<li><input type="checkbox" name="my_check" value="5" />Tianjing</li>
<li><input type="checkbox" name="my_check" value="6" />Chengdu</li>
<li><input type="checkbox" name="my_check" value="7" />Wuhan</li>
<li><input type="checkbox" name="my_check" value="8" />Nanjing</li>
<li><input type="checkbox" name="my_check" value="9" />Shenzhen</li>
<li><input type="checkbox" name="my_check" value="10" />Hangzhou</li>
<li><input type="checkbox" name="my_check" value="11" />Ningbo</li>
<?php
}
?>

3 个答案:

答案 0 :(得分:3)

我首先要为复选框创建一个数组:

$checkBoxes = array(1=>'Beijing',
                    2=>'Shanghai',
                    3=>'Guangzhou',
                    4=>'etc',
                    );

然后我会简单地遍历它们:

<?php
while($row = mysql_fetch_array($query)){
    foreach($checkBoxes as $value => $name) {
        $checked = '';
        if ($row['value'] == $value) $checked = ' checked="true"';
        echo '<li><input type="checkbox" name="my_check" value="' . $value . '"' . $checked . ' />' . $name . '</li>';
    }
}

答案 1 :(得分:2)

试试这个

<?php
// db_connect, mysql_query
while($row = mysql_fetch_array($query)){
// If($row['checkbox']='3)
$checked[$row['checkbox']] = ' checked="checked" ';
?>
<li><input type="checkbox" name="my_check" value="1" <?php echo $checked[1] ?> />Beijing</li>
<li><input type="checkbox" name="my_check" value="2" <?php echo $checked[2] ?> />Shanghai</li>
<li><input type="checkbox" name="my_check" value="3" <?php echo $checked[3] ?> />Guangzhou</li>
<li><input type="checkbox" name="my_check" value="4" <?php echo $checked[4] ?> />Chongqin</li>
<li><input type="checkbox" name="my_check" value="5" <?php echo $checked[5] ?> />Tianjing</li>
<li><input type="checkbox" name="my_check" value="6" <?php echo $checked[6] ?> />Chengdu</li>
<li><input type="checkbox" name="my_check" value="7" <?php echo $checked[7] ?> />Wuhan</li>
<li><input type="checkbox" name="my_check" value="8" <?php echo $checked[8] ?> />Nanjing</li>
<li><input type="checkbox" name="my_check" value="9" <?php echo $checked[9] ?> />Shenzhen</li>
<li><input type="checkbox" name="my_check" value="10" <?php echo $checked[10] ?> />Hangzhou</li>
<li><input type="checkbox" name="my_check" value="11" <?php echo $checked[11] ?> />Ningbo</li>
<?php
}
?>
祝你好运

答案 2 :(得分:1)

    <input type="checkbox" name="my_check" value="3" <?php if($row['checkbox']=='3') checked="yes" ?>/>