我正在运行查询以在搜索页面上从我的数据库中恢复产品。这就是我想要做的事情:
我想在每页的18个分页结果中的6个和12个之后放置一些文字。
如果搜索运行且回复的结果少于12,我只想在6之后显示文本
我如何在我的foreach中使用for-loop工作?
这是我的代码。代码应该放在哪里?
for:
@for ($i = 0; $i < count($products); $i++)
@if (in_array($i,array(6,12,18)) && count($products) >= 12)
<h2> example code </h2>
@elseif ($i == 6 && count($products) < 12)
<h2>example</h2>
@endif
@endfor
的foreach:
@foreach ($products as $product)
<div class="row sing">
<div class="col-md-3">
<a href="{{route('products.single',$product->slug)}}"><div class="img"><div class=" container img-s" style="background-image:url('{{ asset('images/' . $product->image)}}'); background-repeat: no-repeat;
background-size: cover;
background-position: center center;"></div></div></a>
</div>
<div class="col-md-9">
<div class="info-productsearch">
<div class="name-holder">
<h4>{{$product->productname}}</h4>
@if(Count($product->reviews) >0)
<p>Rated : {{number_format($product->reviews->avg('ratings'),2)}}/ 5, from {{$product->reviews->count()}} reviews</p>
@else
<p>Rated : Yet To Be Reviewed</p>
@endif
</div>
<p class="location-det"><i class="icon-map-marker"></i> {{$productt->producttypes}}, {{$sight->productrefcode}}</p>
<p>{{substr(strip_tags($product->prodinfo), 0, 325)}}<a class="find-out-more" href="{{route('products.single',$product->slug)}}"> (...Find out more)</a></p>
</div>
</div>
</div>
@endforeach
答案 0 :(得分:3)
试试这个。
@foreach ($products as $index => $product)
<div class="row sing">
<div class="col-md-3">
<a href="{{route('products.single',$product->slug)}}"><div class="img"><div class=" container img-s" style="background-image:url('{{ asset('images/' . $product->image)}}'); background-repeat: no-repeat; background-size: cover; background-position: center center;"></div></div></a>
</div>
<div class="col-md-9">
<div class="info-productsearch">
<div class="name-holder">
<h4>{{$product->productname}}</h4>
@if(Count($product->reviews) >0)
<p>Rated : {{number_format($product->reviews->avg('ratings'),2)}}/ 5, from {{$product->reviews->count()}} reviews</p>
@else
<p>Rated : Yet To Be Reviewed</p>
@endif
</div>
<p class="location-det"><i class="icon-map-marker"></i> {{$productt->producttypes}}, {{$sight->productrefcode}}</p>
<p>{{substr(strip_tags($product->prodinfo), 0, 325)}}<a class="find-out-more" href="{{route('products.single',$product->slug)}}"> (...Find out more)</a></p>
</div>
</div>
@if (in_array($index, array(5,11,17)) && count($products) >= 12)
<h2> example code </h2>
@elseif ($index == 5 && count($products) < 12)
<h2>example</h2>
@endif
</div>
@endforeach
答案 1 :(得分:1)
我认为最好的方法是使用if条件语句将每个循环组合成一个。
@foreach ($products as $product)
<div class="row sing">
<div class="col-md-3">
<a href="{{route('products.single',$product->slug)}}"><div class="img"><div class=" container img-s" style="background-image:url('{{ asset('images/' . $product->image)}}'); background-repeat: no-repeat;
background-size: cover;
background-position: center center;"></div></div></a>
</div>
<div class="col-md-9">
<div class="info-productsearch">
<div class="name-holder">
<h4>{{$product->productname}}</h4>
@if(Count($product->reviews) >0)
<p>Rated : {{number_format($product->reviews->avg('ratings'),2)}}/ 5, from {{$product->reviews->count()}} reviews</p>
@else
<p>Rated : Yet To Be Reviewed</p>
@endif
</div>
<p class="location-det"><i class="icon-map-marker"></i> {{$productt->producttypes}}, {{$sight->productrefcode}}</p>
<p>{{substr(strip_tags($product->prodinfo), 0, 325)}}<a class="find-out-more" href="{{route('products.single',$product->slug)}}"> (...Find out more)</a></p>
</div>
</div>
@if (in_array($i,array(6,12,18)) && count($products) >= 12)
<h2> example code </h2>
@elseif ($i == 6 && count($products) < 12)
<h2>example</h2>
@endif
编辑:我打开了这个帖子,直到后来才发布答案。看起来adamyi在那个时候发布了一个非常相似的答案。他更正确,因为他修复了你的数组索引问题,我忽略了那些。