【问题标题】:Multiple foreach loops statements多个 foreach 循环语句
【发布时间】:2019-11-23 16:26:49
【问题描述】:

如何处理使用多个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" } 

【问题讨论】:

  • 您正在迭代 $menu_links 而不是 $array_of_values
  • 感谢您的评论。请问我应该改变什么来迭代 $array_of_values ?
  • 你能告诉我这个$menu_links包含什么吗?

标签: php mysql loops codeigniter foreach


【解决方案1】:

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>

谢谢

【讨论】:

  • 怎么样!!太好了,谢谢。
猜你喜欢
  • 2014-02-11
  • 2015-09-15
  • 2021-12-13
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多