【发布时间】:2023-03-23 05:26:02
【问题描述】:
我有 2 个数组,$item 和 $previousItem。 $item 包含所有可能的选择 $previousItem 包含已存储在数据库中的选择。例如,我有带有苹果、橙子、香蕉的复选框,在我的数据库中我记录了用户之前选择了苹果和香蕉。如何填写复选框,以便仅取消选中橙色,而选中其他两个。如果我对 $previousItem 有多个值,则嵌套的 for 循环会创建重复项。这必须仅使用 php 来完成。有没有我可以使用的排序功能?
代码如下:
for($i = 0; $i < $index; $i++)
for($j = 0; $j < $count; $j++)
{
echo '<li>
<!--these are the individual check boxes-->';
echo '<input type="checkbox" name="item[]" id="$item+$count" value="' . $item[$j] . '"'.($previousItem[$i] == $item[$j] ?'checked': '') .'>
<!--these are the values of the check boxes-->
<label for="$item+$count">' . $item[$j] . '</label>
<input type="hidden" name="item_list[]" value="' . $item[$j] . '">';
}
【问题讨论】: