在codeigniter中嵌套的foreach

时间:2017-10-27 11:03:29

标签: mysql codeigniter foreach

我有这样的观点

<?php foreach ($category as $c) { ?>
<li><?php echo $c['name_category'];?></li>

<?php foreach ($sub_category as $sc) {
if($sc['id_category']==$c['id_category']) { ?>
  <ul>
    <li><?php echo $sc['subname_category'];?></li>
  </ul> 
<?php } } }?>

视图的结果是

enter image description here

但我希望看起来像这样

enter image description here

2 个答案:

答案 0 :(得分:0)

在a和css float中包装每个类别:离开它们。

<?php foreach ($category as $c) { ?>

<div>

<li><?php echo $c['name_category'];?></li>

<?php foreach ($sub_category as $sc) {
if($sc['id_category']==$c['id_category']) { ?>
  <ul>
    <li><?php echo $sc['subname_category'];?></li>
  </ul> 
<?php } 

 </div>

 } 
 }?>

答案 1 :(得分:0)

您的外部li未包装子元素。首先,按如下方式修复PHP代码,并将样式display: inline-block添加到所有外部元素。

<?php foreach ($category as $c) { ?>
<li style="display: inline-block"><?php echo $c['name_category'];?>

<?php foreach ($sub_category as $sc) {
if($sc['id_category']==$c['id_category']) { ?>
  <ul>
    <li><?php echo $sc['subname_category'];?></li>
  </ul> 
<?php } } ?> </li> <?php }?>