如何在多个 html 列中显示数据库 table 中的字段?

how display a field from database table in multiple html column?

我有一个 table "category" 有 id,cat_name 字段。假设 table 中有 30 行,我想在 html 三列列表中显示它,每个列表 10 个数据-> 总共 30 个数据。

到目前为止我已经试过了

                 $count = 0;
                 $noOfColms = $catCount / 3;
                                  for ($i=0; $i < 3; $i++) { ?>
                                            <div class="col-md-4">
                                                <ul class="multi-column-dropdown-menu">
                                                    @foreach ($categories as $category)
                                                    @if ($count < $noOfColms)
                                                    <li><a href="#">{{$category->category_name}}</a></li> 
                                                    @else
                                                        <?php  $count = 0; exit;?>
                                                    @endif

                                                    @endforeach
                                                </ul>
                                            </div>
                                        <?php
                                        }

my result 但它就像下图一样。任何帮助将不胜感激。谢谢

每 10 个元素关闭并重新打开 ul 和 div 标签。像这样:

<?php $numOfRows = 10; ?>

<div class="col-md-4">    
  <ul class="multi-column-dropdown-menu">
    @foreach ($categories as $i => $category)
       @if($i % $numOfRows == 0)
         </ul></div><div class='col-md-4'><ul class='multi-column-dropdown-menu'>
       @endif
       <li><a href="#">{{$category->category_name}}</a></li> 
    @endforeach
  </ul>
</div>

关于您的评论,这应该会如您所愿:

<?php 
   $el_counter = 0; 
   $numOfRows = 10;
?>
<?php for($i=0; $i<3; $i++): ?>
  <div class='col-md-4'><ul>
    <?php $current = $el_counter; ?>
    <?php for($el_counter; $el_counter < $current + $numOfRows; $el_counter++): ?>
      <li><a href="#">{{$categories[$el_counter]->category_name}}</a></li> 
    <?php endfor; ?>
  </ul></div>
<?php endfor; ?>