Jump to content

Iteration Problem: Insert Element Every nth Loop


marcus
 Share

Recommended Posts

Hi there,

for some reason I do not succeed in doing a simple iteration (in this case with images):


<?php 
 
$number_of_images = count($pages->get(1012)->images);
 
 
    foreach($pages->get(1012)->images as $image) {
        $thumbnail = $image->size(118,112);
        echo "<a href='{$image->url}'><img class='photo' src='{$thumbnail->url}' alt='{$image->description}' /></a>";
    if ($number_of_images % 3 == 0) { echo "<p>Insert</p>";}
 
    }
 
?>
 
In this case, there a 5 images attached to 1012, if I echo $number_of_images inside the foreach loop it prints "5". But on the other hand this variable doesn't react on my "$number_of_images % 3 == 0", meaning that it does not insert the <p>...-Chunk every 3 images.
 
Can you help me to find the error in my approach or code? :)
 
Thanks in advance!
marcus 
Link to comment
Share on other sites



Been fighting the WYSIWYG editor. 
I usually do something like this:
$index = 0;
foreach($data as $value)
{
    echo "Index: $index\n";
    if ($index % 3 == 0)
    { 
        // do stuff here
    }

    // Add 1 to the index variable 
    $index++;
   
}

You get the picture :)

  • Like 1
Link to comment
Share on other sites

Welcome to the forums Marcus

Your $number_of_images variable will always hold the same value (in this case 5), so the result of the module 3 test will always be false. What you have to do is forget about the images count (you don't really need it for this), and use a simple counter for the loop. With this change, your code would look like this: 

<?php 
 
//$number_of_images = count($pages->get(1012)->images); <- no need for this
 
$i = 1; // <- the counter starts with 1
 
foreach($pages->get(1012)->images as $image) {
    $thumbnail = $image->size(118,112);
    echo "<a href='{$image->url}'><img class='photo' src='{$thumbnail->url}' alt='{$image->description}' /></a>";
    if ($i % 3 == 0) { echo "<p>Insert</p>";} // <- the module test is made against the counter
    i++; // <- update the counter on each iteraction
}
 
?>
 
PS: use the code tag on the editor when writing code. It will look much nicer :)
  • Like 2
Link to comment
Share on other sites

Thank you very much, diogo and arjen! :)

Concerning the Code editor: I tried it first, but the post preview populated the php-code with divs and spans all over (though I was pasting it from SublimeText) - anyway...

Link to comment
Share on other sites

You're welcome. Yep also happened to me the problem with the code after posting the first time, but it was corrected after editing and saving again...

@Pete, sorry to bother you again with this, but the forum is acting a little crazy :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...