Re: [rjbathgate] Foreach, with variation every X
Hi Rob,
Yes, there's a modulus operator that does that.
http://www.php.net/manual/en/language.operators.arithmetic.phpSo, $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