简体   繁体   中英

PHP Foreach Loop not using break

I want to create this loop

<li class="royalSlide">
    <div class="celebFixSlider">
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
    </div>
</li>

<li class="royalSlide">
    <div class="celebFixSlider">
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
        <a href=""><img src="" /></a>
    </div>
</li>

The LI and DIV elements are the first loop, the links are 6 row blocks per the Array.

$imagePath="//site.com/images/celeb1208/";
$productPath="//site.com/product.php?prodref=";
$array = array(
    "632_white" => "celeb1208_317_large.jpg",
     "631_white" => "celeb1208_316_large.jpg",
     "630_white" => "celeb1208_315_large.jpg",
     "629_white" => "celeb1208_314_large.jpg",
     "628_white" => "celeb1208_313_large.jpg",
     "627_white" => "celeb1208_312_large.jpg",
     "532_white" => "celeb1208_311_large.jpg",
     "531_white" => "celeb1208_310_large.jpg",
     "530_white" => "celeb1208_309_large.jpg",
     "529_white" => "celeb1208_308_large.jpg",
     "528_white" => "celeb1208_307_large.jpg",
     "527_white" => "celeb1208_306_large.jpg"
);

$i=0;
foreach ($array as $key => $val)
{      
  $i++; 

        echo '
        <li class="royalSlide">
            <div class="celebFixSlider">
                <a href="'.$productPath.''.$key.'">
                <img src="'.$val.'" />
        </a>';
       if($i==6){
            echo '</div>
            </li>';
            break;            
        }

}

After 6 items, the break just stops the code, it does not pause and recreate the loop within side?? Sorry if I am not clear.

The break; command will stop the loop completly. Maybe you want continue; that will skip what is after the continue keyword and loop again from where it left off.

What you need is no break and no continue.:

$i=0;
echo '<li class="royalSlide"><div class="celebFixSlider">';
foreach ($array as $key => $val)
{
  $i++;
    echo '
            <a href="'.$productPath.''.$key.'">
            <img src="'.$val.'" />
    </a>';
   if($i==6){
        echo '</div>
        </li><li class="royalSlide"><div class="celebFixSlider">';
    }

}
echo '</div></li>';
// getting data ready
foreach ($array as $key => $val) {
  $array[$key] = array(
    "href" => $productPath.$key,
    "img"  => $imagePath.$val
  );
}
$data = array_chunk($array,2);

// printing it out
?>
<?php foreach ($data as $array): ?>
<li class="royalSlide">
    <div class="celebFixSlider">
<?php   foreach ($array as $row): ?>
        <a href="<?=$row['href']?>"><img src="<?=$row['img']?>" /></a>
<?php   endforeach ?>
    </div>
</li>
<?php endforeach ?>

Use "continue" instead of "break";

Break will stop the loop

Thanks

Try this for your loop instead

$i=0;
foreach ($array as $key => $val)
{      
  $i++;
  if(($i % 6) == 0){
    echo '<li class="royalSlide"><div class="celebFixSlider">';
  }

  echo '<a href="'.$productPath.''.$key.'"><img src="'.$val.'" /></a>';

  if(($i % 6) == 0 && $i!=0){
    echo '</div></li>';
  }
}

So you should use this:

 echo '<li class="royalSlide">
        <div class="celebFixSlider">';

 $i=0;
 foreach ($array as $key => $val)
 {
   $i++; 

    echo '
            <a href="'.$productPath.''.$key.'">
            <img src="'.$val.'" />
    </a>';
   if($i==6){
        echo '</div>
        </li>
        <li class="royalSlide">
              <div class="celebFixSlider">';
    }

 }

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