【问题标题】:Bootstrap row class for every 3 columns每 3 列的引导行类
【发布时间】:2014-10-10 08:41:27
【问题描述】:

我想使用 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

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

array_chunk()

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

【问题讨论】:

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


    【解决方案1】:

    你有两个选择。

    1. 使用array_chunk(),首先将结果转换为数组
    2. 使用自定义函数 break_array(),它允许您执行与 array_chunk() 相同的操作,但在对象上

    选项 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
    

    【讨论】:

    • 这是迄今为止最好的答案
    • 一个很好的解决方案,当我的模板没有尽可能原始时,我总是大惊小怪!
    【解决方案2】:

    如果计数为 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
    ?>
    

    【讨论】:

      【解决方案3】:
      <?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!
      
          }
      
      ?>
      

      【讨论】:

        【解决方案4】:
        <?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
            }
        }   
        
        ?>
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            //初始化计数为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
            

            【讨论】:

              猜你喜欢
              • 2017-03-26
              • 2015-01-28
              • 2017-05-19
              • 2018-03-09
              • 1970-01-01
              • 2020-06-16
              • 2020-02-23
              • 1970-01-01
              • 2014-03-04
              相关资源
              最近更新 更多