多个foreach循环语句

时间:2019-07-14 23:00:10

标签: php mysql loops codeigniter foreach

如何使用多个foreach循环进行处理?但是,它只应给出4时才给出16行-因为它会重复多次输入。简而言之,我从MySQL中读取了所有子类别并标记了先前已通过复选框选中的子类别,然后全部更新。

我在这里做错了-感谢您的帮助。谢谢。

目前,我有:

<div class="form-group">
    <div class="col-xm-12">
        <div class="table-responsive">
            <table class="table table-bordered table-striped" role="grid">
                <tbody>
                <tr>
                    <td>
                        <?php $valuesub = ($page->subcat_recip_id);
                        $array_of_values = explode(",", $valuesub);

                        foreach ($menu_links as $item):

                        if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0"):?>
                        <?php foreach($array_of_values as $dt):  ?>

                        <input type="checkbox" name="subcat_recip_id" class="square-purple"
                               value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($dt, $item)) ? "CHECKED" : ""?>> <?=html_escape($item["title"]);  ?>
                        &nbsp;
                        <?php  endforeach;endif;endforeach; ?>

                    </td><?php echo html_escape($valuesub); ?>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

示例数组$array_of_values包含:

array(4) { [0]=> string(10) "Appetizers" [1]=> string(9) "Beverages" [2]=> string(7) "Dessert" [3]=> string(5) "Bread" } 

1 个答案:

答案 0 :(得分:0)

启动$ dt必须在输入复选框中循环, 看这段代码:

<td> 
<?php foreach ($menu_links as $item) :
$valuesub = explode(",",$page->subcat_recip_id); ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?php foreach($valuesub as $dt): echo ($dt==$item['title'])?"CHECKED":""; endforeach;?> > <?=html_escape($item["title"]);?>&nbsp;
<?php endforeach; ?> 
</td>

谢谢