首页 > 解决方案 > 多个 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" } 

标签: phpmysqlloopscodeigniterforeach

解决方案


aboot $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>

谢谢


推荐阅读