Reputation: 4343
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,
Upvotes: 0
Views: 1185
Reputation: 18853
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.
Upvotes: 2
Reputation: 72652
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>');
}
Upvotes: 0