如何使用foreach进行for循环

I'm running a query to bring back products from my database on a search page. Here is what I want to do:

  • I want to place some text after 6 and 12 of the 18 paginated results on each page.

  • If a search is run and less than 12 results come back, I want to show the text after 6 only

How do I work in my for-loop within my foreach?

Here is my code. Where should the code be placed?

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

Try this.

@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

I think the best way to do this would be to combine these into one for each loop with an if conditional statement.

@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

Edit: I opened this thread and didn't post an answer until later on. Looks like adamyi has posted a very similar answer in that time. His is more correct because he fixed your array index issues, I overlooked those.