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

时间:2016-12-29 12:36:20

标签: html mysql laravel-5.3

我有一个包含id,cat_name字段的表“category”。假设表中有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 但它就像下图。任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

关闭并重新打开每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; ?>