php - 多个 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"]); ?>
<?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" }
解决方案
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"]);?>
<?php endforeach; ?>
</td>
谢谢
推荐阅读
- c# - 使用 Wordpress 调试 Asp net core oauth 登录并正确注销
- reactjs - 无法导入 css 文件说错误意外令牌
- python - Python Bash 脚本启动
- azure - Azure Function Service Bus Trigger throws error 启动操作期间发生主机错误
- python - 非线性测试 NaN 错误
- rust - 丢下一个 tokio::pin!-ned 异步块不释放借用
- spring - 如何在 Spring Security 中扩展登录用户信息?
- javascript - Node.js 内部 500 服务器错误“无法读取未定义的 id”
- swift - UIActivityIndicator 不显示
- java - Spring-Maven 应用程序无法执行“mvn spring-boot:run”