0

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.

6
  • it works as it should, what is your question then? Commented Mar 13, 2012 at 15:01
  • So you want to create a new <ul> every 6 <li>s? Commented Mar 13, 2012 at 15:01
  • You are not clear... Anyway, break is doing what it's supposed to: exiting the loop. Commented Mar 13, 2012 at 15:02
  • what are you trying to achieve with this good. please explain in details. break statement will cause to exit from the for loop as well. what do you expect??, Commented Mar 13, 2012 at 15:02
  • I want to create the block of code above, so it creates a LI and DIV with 6 items every 6 Commented Mar 13, 2012 at 15:04

5 Answers 5

3
// 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 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Like the name change. And this is some smart code, cant believe I did not approach it like this.
Aha thanks. It's always hard to spot these typos when writing in the textarea without running the code :)
3

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>';

1 Comment

continue doesn't make much more sense than break at this point. Actually, it makes less sense since it's a no-op in this case.
2

Use "continue" instead of "break";

Break will stop the loop

Thanks

Comments

0

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">';
    }

 }

Comments

0

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>';
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.