显示基于数组的复选框列表

时间:2013-02-25 05:40:26

标签: php mysql arrays checkbox selected

我有一个显示在下面的复选框列表。这将显示所有接触器,并允许通过复选框选择它们。

<?php
 $query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
        <option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
            </select></li>

我有一个数组保存在另一个地方,我想生成上面的列表,但下面的数组中的项目已经检查/选中。

<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");

while($row = mysql_fetch_array($result))
  {
    $contractors = unserialize($row['contractors']);
  foreach ($contractors as $contractor)
  echo "" . htmlspecialchars ($contractor).' - ';
?>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

试试这个:

<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>