3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 30   (RSS)

Hi Jesus,

A fairly easy way to specify something to happen every "X" records is using the modulo operator (%), where $a % $b returns the remainder of $a divided by $b. For example: 5 % 3 = 2.

If you add a counter to your loop and apply a modulo of 3 to it, it will be able to tell you which position the item is, as "X % 3" will always return 0, 1, or 2. If you're using a regular CMSB list section, it could look something like this:

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

    $class = '';
    if ($counter % 3 == 0) {
      $class = 'last_column';

  <div class="item <?php echo $class; ?>">

<?php endforeach; ?>

You'll need to adapt this to your own code, but hopefully, this gets you going in the right direction.

Let me know if you have any questions!


PHP Programmer

Thank you daniel,

Will try to make a few test during the weekend with this code, will let you know how it goes!