简体   繁体   中英

foreach loop conditional execution in php

<div class="col-lg-4 col-md-4">
     <div class="media-banner mb-3 mb-lg-0 banner-bottom">
        <a href="{!! URL::to('product-list/8/women') !!}"  class="banner-container">
        <img src="{{ asset('frontend/img/women.jpg') }}" alt="" class="img-fluid" >
        </a>
     </div>
     <div class="media-banner mb-3 mb-lg-0 banner-bottom">
        <a href="{!! URL::to('product-list/10/girls') !!}"  class="banner-container">
        <img src="{{ asset('frontend/img/girls.jpg') }}" alt="" class="img-fluid">
        </a>
     </div>
  </div>

  <div class="col-lg-4 col-md-4">
     <div class="media-banner mb-3 mb-lg-0">
        <a href="{!! URL::to('product-list/9/men') !!}"  class="banner-container">
        <img src="{{ asset('frontend/img/men.jpg') }}" alt="" class="img-fluid" >
        </a>
     </div>
  </div>

  <div class="col-lg-4 col-md-4">
     <div class="media-banner mb-3 mb-lg-0 banner-top">
        <a href="{!! URL::to('product-list/11/boys') !!}"  class="banner-container">
        <img src="{{ asset('frontend/img/boys.jpg') }}" alt="" class="img-fluid">
        </a>
     </div>
     <div class="media-banner mb-3 mb-lg-0 banner-top">
        <a href="{!! URL::to('product-list/12/hijab') !!}"  class="banner-container">
        <img src="{{ asset('frontend/img/b.jpg') }}" alt="" class="img-fluid">
        </a>
     </div>
  </div>

Here is the sample HTML I need to make dynamic for for first 2 items, there is a col-MD-4 class for 3rd item col-MD-4 class alone, and for the 4th and 5th item another col-MD-4 class

I have tried to solve this and my code is

@php $slno = 1;  @endphp
  @foreach($categories as $n)
  @php $key = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "-", $n->name)) @endphp
  @if($slno!=3)
   <div class="col-lg-4 col-md-4">
     <div class="media-banner mb-3 mb-lg-0 @if($slno == 1 || $slno == 2){{ 'banner-bottom' }} @elseif($slno == 3 || $slno == 4){{ 'banner-top' }}@endif">
        <a href="{!! URL::to('product-list/'.$n->id.'/'.$key) !!}"  class="banner-container">
        <img src="{{ asset('categories/'.$n->image) }}" alt="" class="img-fluid" >
        </a>
     </div>
  </div>
  @else
  <div class="col-lg-4 col-md-4">
     <div class="media-banner mb-3 mb-lg-0">
        <a href="{!! URL::to('product-list/'.$n->id.'/'.$key) !!}" class="banner-container">
        <img src="{{ asset('categories/'.$n->image) }}" alt="" class="img-fluid" >
        </a>
     </div>
  </div>
  @endif
  @php $slno++; @endphp
  @endforeach

But surely I'm wrong.

I need help. It will actually look like this. 这就是它的样子

this code may help you, follow like this

for ($i=1; $i <= 5; $i++) { 
    echo $i;
    if($i == 1 || $i == 2){
      if($i == 1){
        echo "col-md-4";
      }
        echo "Bottom";echo "<br>";
        if($i == 2){
        echo "col-md-4 close tag";
      }
    }else if($i == 4 || $i == 5){
      if($i == 4){
        echo "col-md-4";
      }
        echo "Top";echo "<br>";
        if($i == 5){
        echo "col-md-4 close tag";
      }
    }else{
        echo "Center";echo "<br>";
    }
}

This code may work

    //Enter your code here, enjoy!


   $de_chunked = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 , 11 , 12, 13, 14];


 $d = array_chunk($de_chunked,3);


foreach($d as $k => $val)
{
  $i = 1;
   foreach($val as $ke => $vals)
   if($i++ == 3)
      {
        echo 'Yes   ';
      }else{
        echo "No   ";
      }

}

This is the result: No No Yes No No Yes No No Yes No No Yes No No

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM