我有一个显示一些旧数据的while循环。我最近在数据库中添加了一个新的tablerow,其中包含一个字符串,该字符串在每个逗号处分解为一个数组。
然后我使用for循环将数组值显示到列表中,并使用包含echo in while循环数据中的列表的变量。
我的代码(仅限相关部分):
while($row_items = mysqli_fetch_array($res_items, MYSQLI_ASSOC)) {
if($row_items['checkpoints'] != ''){
$check = explode(',', $row_items['checkpoints']);
}else{
$check = '';
}
foreach($check as $list){
$checklist .= '<li>'.$list.'</li>';
}
echo '
<div class="col-lg-2 col-md-3 col-sm-6 col-xs-6 portf">
<a class="fancybox" href="../../catalogus/'.$row_items['afbeelding'].'">
<div class="gallery-item">
<div class="image">
<img src="../../catalogus_icons/'.$row_items['afbeelding'].'.jpg" alt="" style="border:1px solid #ccc;" class="img-responsive" />
</div>
<div class="bottom-info">
<div class="name">'.$row_items['naam'].'</div>
<div>'.$checklist.'</div>
<button class="contact_button buttoncontact btn-primary" style="border-radius:2px;">
Contact
<span class="icon-mail-alt"></span>
</button>
</div>
</div>
</a>
</div>';
}
但是这会在每个div输出上显示相同的列表(while循环结果)。我添加了1个要测试的列表,它将该列表添加到每个项目,而不仅仅是具有列表的项目。我怎样才能确保它只将列表添加到正确的结果中?
$row_items['checkpoints'];
包含:'goedkoop,snel,test'
答案 0 :(得分:1)
这很可能是因为您没有在循环迭代之间清除$checklist
变量。您只需在每次迭代时添加到同一列表中。
while ($row_items = mysqli_fetch_assoc($res_items)) {
$checklist = '';
if ($row_items['checkpoints'] != '') {
$check = explode(',', $row_items['checkpoints']);
foreach($check as $list) {
$checklist .= '<li>'.$list.'</li>';
}
}
echo '
<div class="col-lg-2 col-md-3 col-sm-6 col-xs-6 portf">
<a class="fancybox" href="../../catalogus/'.$row_items['afbeelding'].'">
<div class="gallery-item">
<div class="image">
<img src="../../catalogus_icons/'.$row_items['afbeelding'].'.jpg" alt="" style="border:1px solid #ccc;" class="img-responsive" />
</div>
<div class="bottom-info">
<div class="name">'.$row_items['naam'].'</div>
<div>'.$checklist.'</div>
<button class="contact_button buttoncontact btn-primary" style="border-radius:2px;">
Contact
<span class="icon-mail-alt"></span>
</button>
</div>
</div>
</a>
</div>';
}