0

如何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
}
?>
4

3 回答 3

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>';
    }
}
于 2012-04-14T12:28:14.577 回答
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
}
?>

祝你好运

于 2012-04-14T12:33:11.110 回答
1
    <input type="checkbox" name="my_check" value="3" <?php if($row['checkbox']=='3') checked="yes" ?>/>
于 2012-04-14T12:25:00.667 回答