Kirsten Phukon
Kirsten Phukon

Reputation: 314

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

I have a table "category" having id, cat_name fields. Suppose there are 30 rows in the table, i want to show it in html list of three columns 10 data each list-> total 30 data.

i have tried it so far

                 $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 but it is like the image below. Any help will be greatly appreciated. Thanks

Upvotes: 0

Views: 81

Answers (1)

mparafiniuk
mparafiniuk

Reputation: 320

Just close and reopen ul and div tags every 10 elements. Something like this:

<?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>

Regarding your comments this should work as you expect:

<?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; ?>

Upvotes: 1

Related Questions