Jump to content

Recommended Posts

Posted

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 
Posted


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
Posted

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
Posted

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...

Posted

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 :(

Posted
Arjen, 0 % 3 = 0, you want to insert the <p> on the first image? ;)

:)

I should've typed it in an editor, but instead I've been copying and pasting in the WYSINWYG editor.

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
×
×
  • Create New...