如何有一个嵌套的foreach循环没有重复的子记录



我是一个初学者和新的Codeigniter3。我做了一个清单系统,它有两个单选按钮,好和不好。一个人会检查是否所有的选项都是正确的。如果在任何选项中有问题,他都会选择not fine。然后是"问题"。页面,我只显示他选择的选项"不太好"。他将能够在每个问题上添加多个评论。这部分我都做过了。我面临的唯一问题是如何显示每个选项下的评论?现在发生的事是为了eg。我有3个问题,每个问题有一个评论,但仍然会显示每个选项下的所有评论。我知道这是嵌套循环的正常行为,但我不知道如何在各自的问题下显示注释。

<?php foreach($issues as $i=>$issue) : ?>
<div class="form-row col-md-5">
<!-- Radio Group -->
<div class="form-group col-md-6">     
<h6><?php echo $issue['checks']; ?></h6>
</div>

<div class="form-group col-md-6">
<?php foreach($comments as $comment) : ?>
<label class="input-group input-group-sm mb-4">
<input type="hidden" name="chk_value[<?php echo $i; ?>]" value="<?php echo $issue['checks']; ?>">
<input type="text" name="comment[<?php echo $i; ?>]" value="<?php echo $comment['comment']; ?>" class="form-control">
</label>
<?php endforeach; ?>
</div>

</div>
<div class="col-md-1"></div>
<?php endforeach; ?>

问题似乎是问题和注释之间缺乏联系。我不确定你的数据结构是什么,但你应该让它嵌套,然后像这样使用它

foreach($issues as $issue){
foreach($issue['comments'] as $comment){
//show comment
}
}

或者有某种可以链接它们的'id'

foreach($issues as $issue){
foreach($comments as $comment){
if($comment['issue_id'] == $issue['id']){
//show comment
}
}
}

最新更新