Bootstrap class for every 3 columns

I want to print 12 articles with pagers using bootstrap 3 theming:

@foreach($category->articles()->orderBy('created_at', 'DESC')->paginate(12) as $article) <div class="col-md-4"> <a href="/article/{{ $article->slug }}"><img class="img-responsive" src="/imagecache/mainart/{{ $article->image }}" /></a> <p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p> </div><!--/col-md-4--> @endforeach 

However, I need to print a div with the class "row" with three columns inside.

 array_chunk() 

will not work in my case, because I print articles related to the category (one-to-many relationship), and this is an object, not an array.

+8
twitter-bootstrap twitter-bootstrap-3 laravel laravel-4
source share
6 answers

You have two options.

  • Use array_chunk() by converting the results to an array first
  • Use the break_array() custom function, which allows you to do the same thing as array_chunk() , but on objects

Option 1:

 @foreach (array_chunk($category->articles()->orderBy('created_at', 'DESC')->paginate(12)->toArray()['data'], 3, true) as $column) <div class="row"> @foreach ($column as $article) <div class="col-md-4"> <p>{{ strip_tags(str_limit($article['body'], $limit = 90, $end = '...')) }}</p> </div @endforeach </div> @endforeach 

Option 2:

Put this in a helper function somewhere in your application:

 function break_array($array, $page_size) { $arrays = array(); $i = 0; foreach ($array as $index => $item) { if ($i++ % $page_size == 0) { $arrays[] = array(); $current = & $arrays[count($arrays)-1]; } $current[] = $item; } return $arrays; } 

Then, in your opinion:

 @foreach (break_array($category->articles()->orderBy('created_at', 'DESC')->paginate(12), 3) as $column) <div class="row"> @foreach ($column as $article) <div class="col-md-4"> <p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p> </div @endforeach </div> @endforeach 
+10
source share

Create a variable count and echo enter a new row if the counter is 0 or divisible by 3. I removed most of your code from the example below, you will need to add your foreach as well as your div content.

  <?php $count = 0; { //foreach as shown in question if($count==0 OR is_int($count/3)){ echo '<div class="row">'; } ?> <div class="col-md-4"> <!--content--> </div> <?php if($count==0 OR is_int($count/3)){ echo '</div>'; } $count++; } //end foreach ?> 
+7
source share
 <?php $num = 1; $breaker = 3; //How many cols inside a row? foreach($a as $b) { if ($num == 1) echo '<div class="row">'; //First col, so open the row. echo '<div class="col-sm-4">Test</div>'; $num++; if ($num > $breaker) { echo '</div>'; $num = 1; } // The num arrived at the break-point. Close the row! } ?> 
+1
source share
 <?php $c = 0; $breaker = 3; foreach ($cols as $col) { if ($c == 0 || $c%$breaker == 0) { ?><div class="row"><?php } ?> <div class="col-md-4"> <!-- content of column here --> </div> <?php $c++; if ($c%$breaker == 0) { ?></div><?php } } ?> 
0
source share

The one Dan suggested will incorrectly split the columns. A more correct option would look like this:

 $count = 0; foreach ($rows_output as $row) { if ($columns==1) $output .= '<li'.$active.'>'.$elem.'</li>'; else { if ($count==0) $output .= '<div class="row">'; if (is_int($count / $columns)) $output .= '</div><div class="row">'; $output .= "<div class='col-md-".(12/$columns)."'>".$elem."</div>"; if ($count==count($rows_output)) $output .= '</div>'; } $count++; } 
0
source share

// initialize the account to 4.5

 @foreach ($images as $image ) @if($count==0 OR is_int($count/3)) <?php echo '<div class="row">';?> @endif <div class="col-md-3"> <div class="well" style="background:white;"> <img src="{{ url('images/'.$image->filepath) }}" alt="" class="" height="100" width="100" ></br> {{ $image->title }}<br> {{$image->filepath }} </div> </div> @if($count==0 OR is_int($count/3)) <?php echo '</div>' ;?> @endif <?php $count++;?> @endforeach 
0
source share

All Articles