Foreach, with variation every X

5 posts by 2 authors in: Forums > CMS Builder
Last Post: December 15, 2008   (RSS)

By rjbathgate - November 16, 2008 - edited: November 17, 2008

Hey,

Say I have 10 records in a table... and I am displaying them on a list page (so using foreach)...

say:
foreach...
<div class="bucket">
content
</div>
endforeach


Can I make it do something different for each (say) 3rd record... i.e. so record 3,6 and 9 display as:
foreach...
<div class="bucketthree">
content
</div>
endforeach


So basically the page renders like this:

<div class="bucket">
content
</div>
<div class="bucket">
content
</div>
<div class="bucketthree">
content
</div>
<div class="bucket">
content
</div>
<div class="bucket">
content
</div>
<div class="bucketthree">
content
</div>


Is this at all possible?

As I write this, I have thought that by adding a field into CMS (such as a checkbox) which is checked for every 3rd record, and then the foreach works as:

foreach...
if checkbox is not checked
<div class="bucket">
content
</div>
endforeach
endif
if checkbox is check
<div class="bucketthree">
content
</div>
endif
endforeach


But that's not ideal having to control it through CMS...

Is there any other way?

Many thanks in advance
Rob


Ive just thought of a potential way of doing it...

foreach...
if num is divisible by 3
<div class="bucketthree">
content
</div>
else (if num is not divisible by 3)
<div class="bucket">
content
</div>
endforeach


Is there such a statement to determine IF a numeric value is divisible by x?

Cheers

Re: [rjbathgate] Foreach, with variation every X

By Dave - November 17, 2008

Hi Rob,

Yes, there's a modulus operator that does that. http://www.php.net/manual/en/language.operators.arithmetic.php

So, $a % $b gives you the remainder of $a divided by $b. So if you have a counter you can test if it cleanly divides into 3 like this:

if (($counter % 3) == 0) { ...

Or, if the remainder of your counter divided by 3 is zero.

Or, in one line:

<?php foreach ... ?>
<?php $isThird = @++$myCounter % 3 == 0; ?>
<?php
if ($isThird) { $class = "bucket"; }
else { $class = "bucketThird"; }
?>

or

<?php if ($isThird): ?> ... <?php endif ?>

Sometimes it's easier (simpler to understand) to just have a counter and reset it when it gets to 3, but both would work:

$counter = 0;
foreach ...
$counter++;

if ($counter == 3) {
$counter = 0;
print "3rd row";
}

Let me know if one of those solutions works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [rjbathgate] Foreach, with variation every X

Hi again,

Just an update on what worked for me:

<?php $counter = 1; ?>
<?php foreach ($productsRecords as $record): ?>

<?php if ($counter <> 4): ?>
<p>Normal</p>
<?php endif ?>

<?php if ($counter == 4): ?>
<p>This is forth</p>
<?php $counter = 0; ?>
<?php endif ?>

<?php $counter++; ?>
<?php endforeach; ?>


Many thanks Dave
Rob

Re: [rjbathgate] Foreach, with variation every X

By Dave - December 15, 2008

Glad it's working. Thanks for sharing your code! :)
Dave Edis - Senior Developer
interactivetools.com