0

I have a bit of a weird issue.

I want to add a div tag around remaining results after a certain amount is reached in my foreach loop.

So, after the loop returns 6 results, it wraps the rest in

The code i have to return results at the moment is:

foreach ($fpbanners as $banners):
    <img src="image.jpg" alt="image description" width="773" height="432" />
endforeach;

At the end i need to source code to look something like the following so all results after the 6th is wrapped in the div tag

<img src="image.jpg" alt="image description" width="773" height="432" />
<img src="image.jpg" alt="image description" width="773" height="432" />
<img src="image.jpg" alt="image description" width="773" height="432" />
<img src="image.jpg" alt="image description" width="773" height="432" />
<img src="image.jpg" alt="image description" width="773" height="432" />
<img src="image.jpg" alt="image description" width="773" height="432" />
<div class="test">
    <img src="image.jpg" alt="image description" width="773" height="432" />
    <img src="image.jpg" alt="image description" width="773" height="432" />
    <img src="image.jpg" alt="image description" width="773" height="432" />
</div>

Any help would be greatly appreciated.

Cheers,

2 Answers 2

2

You are going to want to use the modulus operator: wiki link

$i=0;
<div class="test">
foreach ($fpbanners as $banners):
    if ($i % 6 == 0) :
         </div><div class="test">
    endif;
    <img src="image.jpg" alt="image description" width="773" height="432" />
    $i++;
endforeach;
</div>

Untested, but some tinkering should get you what you want.

If you do not want it to repeat on every 6th result, then you just want the == and not the modulus operator (%).


UPDATE

While loop example:

$i=0;
$max = count($fpbanners);
echo '<div class="test">'; 
while ($i < $max) {
     if ($i % 6 == 0) {
          echo '</div><div class="test">';
     }

     echo '<img src="' . $fpbanners[$i] . '" alt="image description" width="773" height="432" />';
     $i++;
}

Not knowing how your array is structured etc, that is the best I can do for you, a rough example.

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

2 Comments

Is there a way to do the same thing in a while loop?
Beautiful ... Works fantastic .. Thanks
0

Use either a counter

$i = 0;
foreach ($fpbanners as $banners) {
    $i++

    if ($i > 6) {
        print('<div class="test">');
    }

    print('<img src="image.jpg" alt="image description" width="773" height="432" />');
}

if ($i > 6) {
    print('</div>');
}

Or if the array contains nice numeric keys (read 0, 1, 2, 3, etc)

foreach ($fpbanners as $index=>$banners) {
    if ($index > 5) {
        print('<div class="test">');
    }

    print('<img src="image.jpg" alt="image description" width="773" height="432" />');
}

if ($index > 5) {
    print('</div>');
}

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.