Code to effect every 3rd foreach
3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 12, 2010 (RSS)
By (Deleted User) - July 10, 2010
Hello,
I have a portfolio page where there are 3 posts listed across the screen. the third one on each row has no right margin.
So effectively the 3rd, 6th, 9th etc post has no right margin.
How can I create a list with 'foreach' but it display a different one every third?
so, for the first and second in each row:
Then every third remove the class 'left' from the div:
Any ideas?
Thanks Jono
I have a portfolio page where there are 3 posts listed across the screen. the third one on each row has no right margin.
So effectively the 3rd, 6th, 9th etc post has no right margin.
How can I create a list with 'foreach' but it display a different one every third?
so, for the first and second in each row:
<?php foreach ($blogRecords as $record): ?>
<div class="box left">
<img src="<?php echo $record['list_image_url'] ?>" alt="<?php echo $record['list_image_desc'] ?>" />
<h2><?php echo $record['title'] ?></h2>
<p><?php echo $record['list_image_descs_t'] ?></p>
</div>
<?php endforeach ?>
Then every third remove the class 'left' from the div:
<?php foreach ($blogRecords as $record): ?>
<div class="box">
<img src="<?php echo $record['list_image_url'] ?>" alt="<?php echo $record['list_image_desc'] ?>" />
<h2><?php echo $record['title'] ?></h2>
<p><?php echo $record['list_image_descs_t'] ?></p>
</div>
<?php endforeach ?>
Any ideas?
Thanks Jono
Re: [jonoc73] Code to effect every 3rd foreach
By Jason - July 12, 2010
Hi Jono,
One thing you could try would be to have a counter variable that counts off each 3 records, and outputs a different div tag on the third.
Try this code:
Hope this helps
One thing you could try would be to have a counter variable that counts off each 3 records, and outputs a different div tag on the third.
Try this code:
<?php $count=1;?>
<?php foreach ($blogRecords as $record): ?>
<?php if($count!=3): ?>
<div class="box left">
<?php else: ?>
<div class="box">
<?php $count=0;?>
<?php endif ?>
<img src="<?php echo $record['list_image_url'] ?>" alt="<?php echo $record['list_image_desc'] ?>" />
<h2><?php echo $record['title'] ?></h2>
<p><?php echo $record['list_image_descs_t'] ?></p>
</div>
<?php $count++; ?>
<?php endforeach ?>
Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Code to effect every 3rd foreach
By (Deleted User) - July 12, 2010
As usual Jason, works perfectly.
Thanks Jono
Thanks Jono