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:

<?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: [Jason] Code to effect every 3rd foreach

By (Deleted User) - July 12, 2010

As usual Jason, works perfectly.

Thanks Jono