donglv5269 2017-08-16 19:05
浏览 147

如何使用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
  • 写回答

2条回答 默认 最新

  • douxian7808 2017-08-16 20:08
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了