laravel 每 3 列的 Bootstrap 行类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25346290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap row class for every 3 columns
提问by Alexander Kim
i want to print 12 articles with pagers using bootstrap 3 theming:
我想使用 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
However, i need to print div with class "row" with 3 columns inside.
但是,我需要使用类“row”打印 div,里面有 3 列。
array_chunk()
won't work in my case, because i am printing category related articles (one to many relationship) and it's object, not array.
在我的情况下不起作用,因为我正在打印与类别相关的文章(一对多关系)并且它是对象,而不是数组。
回答by Laurence
You have two options.
你有两个选择。
- Use
array_chunk()
by converting your results to an array first - Use a custom function
break_array()
which allows you to do the same thing asarray_chunk()
but on objects
- 使用
array_chunk()
首先将你的结果到一个数组 - 使用自定义函数
break_array()
,它允许您执行与array_chunk()
对象相同的操作
Option 1:
选项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:
选项 2:
Place 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 view:
那么在你看来:
@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
回答by Dan
Create a count variable and echo a new row
if the count is 0 or is divisible by 3. I removed most of your code from the below example, you will have to add back your foreach
loop as well as your div
content.
row
如果计数为 0 或可被 3 整除,则创建一个计数变量并回显一个新变量。我从下面的示例中删除了您的大部分代码,您必须重新添加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
?>
回答by Cas Bloem
<?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!
}
?>
回答by tribulant
<?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
}
}
?>
回答by simon
The one suggested by Dan will produce wrong separation into columns. More correct variant will look like:
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++;
}
回答by james kandau
//initialize count to 4.5
//初始化计数为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