我有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] . '">';
}
繁花如伊