0

Is it possible to control a for-loop when it reaches a certain condition?

Explanation:

I'm retrieving the folder path to a collection of images from a database: these images are then printed out via a for-loop. What I would need to do is control how these images are displayed on the page (say, 5 images per row). As of now, the for-loop prints out 40 images in a single row, which makes you scroll to the furthest right of the page.

Is there a solution for controlling the for-loop, as in for instance, after 5 successful loops, echo out a < br >? Here's a vulgar thought:

for ($i = 1; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path']; //since it's an array
echo '<img src="' . $folder_path . '">';

//pseudocode
if ($i == 5) {
echo '<br>';
...continue with the loop
}
}

I know the pseudocode looks crazy, but that's pretty much what I need to do: loop for x amount of instances, add something, then continue.

1
  • 3
    Use the modulo operator, as in $i % 5 == 0 to do something every fifth iteration. Commented Aug 31, 2015 at 3:13

2 Answers 2

1

As per @m69's comment, the best option would be to use the % (modulus) comparison operator. As explained by the PHP docs:

$a % $b returns the remainder of $a divided by $b.

So, in your case:

for ($i = 0; $i < $rows; $i++) {

    $path = $image[$i];
    $folder_path = $path['folder_path']; //since it's an array
    echo '<img src="' . $folder_path . '">';

    if ($i % 5 == 0) { //do this if $i divided by 5 has a remainder of 0
        echo '<br>';
    }

}

As a side note, you should set $i to 0 at the beginning of your for loop, assuming $rows is set to the number of rows returned from your query. Setting it to 1 will keep it from iterating through the last row, because $i will == 40 (assuming 40 rows), and so will NOT be < 40.

Sign up to request clarification or add additional context in comments.

3 Comments

What the modulus is doing here is that it adds the < br > prior to echoing out the images and the result remains the same: the images are printed in a single row, but they are pushed down, due to the use of modulus. It seems that in echoing out the array, the images are being treated as one element. I tried with foreach, but got the same result. The folder path is being retrieved with fetchAll(), then the relevant array position is stored in a var. I solved the issue though, with css (added a class and float: left). I also removed the < td > that wrapped the img and now it works.
If you have solved the problem and none of these answers fully answer your question, please either: 1) post your own answer and accept it as the best answer, or 2) consider deleting your question from SO.
I can't delete it because the thread has answers. I tagged the question for deletion. Even though I solved my issue, my solution doesn't answer the question: namely, to separate the images between (as with < br > with the for-loop. I tested doing this for string and it worked just like you guys said it would. It just seems not to be working for the images it echoes out from folder.
1

The same loop. The condition for inserting the
is ($i % 5 == 0), which means (if this element is the fifth one of his series) will be useful for you.

<?php
for ($i = 1; $i < $rows; $i++) {
  $path = $image[$i];
  $folder_path = $path['folder_path']; 
  echo '<img src="' . $folder_path . '">';

  if ($i % 5 == 0) {
    echo '<br>';
  }
}

1 Comment

Please explain a litle bit what you do in your code.

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.