每3列的Bootstrap行类

时间:2014-08-17 04:22:44

标签: twitter-bootstrap laravel laravel-4 twitter-bootstrap-3

我想使用bootstrap 3主题使用寻呼机打印12篇文章:

@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

但是,我需要打印带有“row”类的div,里面有3列。

array_chunk()

在我的情况下不起作用,因为我打印类别相关的文章(一对多的关系)和它的对象,而不是数组。

6 个答案:

答案 0 :(得分:10)

您有两种选择。

  1. 首先将结果转换为数组
  2. ,然后使用array_chunk()
  3. 使用自定义函数break_array(),它允许您执行与array_chunk()相同的操作,但对象
  4. 选项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
    

    选项2:

    将它放在应用程序某处的辅助函数中:

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

    然后在你看来:

    @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
    

答案 1 :(得分:7)

如果计数为0或者可以被3整除,则创建一个计数变量并回显新的row。我从以下示例中删除了大部分代码,您必须添加回foreach循环以及div内容。

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

答案 2 :(得分:1)

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

    }

?>

答案 3 :(得分:0)

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

?>

答案 4 :(得分:0)

Dan建议的那个会产生错误的分离。 更正确的变体看起来像:

$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++;    
}

答案 5 :(得分:0)

//将count初始化为4.5

dropwizard-java8