 | |  |
 |

rjbathgate
User
Nov 16, 2008, 6:42 PM
Post #1 of 5
(438 views)
Shortcut
|
|
Foreach, with variation every X
|
Can't Post
|
|
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
(This post was edited by rjbathgate on Nov 16, 2008, 10:06 PM)
|
|
|  |
 |

Dave
Staff
/ Moderator

Nov 17, 2008, 10:20 AM
Post #2 of 5
(423 views)
Shortcut
|
|
Re: [rjbathgate] Foreach, with variation every X
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

rjbathgate
User
Nov 17, 2008, 5:42 PM
Post #3 of 5
(416 views)
Shortcut
|
|
Re: [Dave] Foreach, with variation every X
[In reply to]
|
Can't Post
|
|
Cheers Dave, It's for something we'll be integrating in the near future so I'll reply here with what worked for me, when we do it... It will be very handy when having divs displaying horizontally, to be able to give the last div on each line a different class style (e.g. when each div has right margin, the last one on each line needs no right margin in order to main symmetry with the main template). Cheers again Rob
(This post was edited by rjbathgate on Nov 17, 2008, 5:45 PM)
|
|
|  |
 |

rjbathgate
User
Dec 15, 2008, 12:38 PM
Post #4 of 5
(204 views)
Shortcut
|
|
Re: [rjbathgate] Foreach, with variation every X
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

Dave
Staff
/ Moderator

Dec 15, 2008, 1:09 PM
Post #5 of 5
(202 views)
Shortcut
|
|
Re: [rjbathgate] Foreach, with variation every X
[In reply to]
|
Can't Post
|
|
Glad it's working. Thanks for sharing your code! :) Dave Edis - Senior Developer interactivetools.com
|
|
|  |
|