List 3 records per line

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

By daniel - April 30, 2019

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): ?>

  <?php 
    $class = '';
    if ($counter % 3 == 0) {
      $class = 'last_column';
    }
    $counter++;
  ?>

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

<?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!

Thanks,

Daniel
Technical Lead
interactivetools.com

By Jesus - April 30, 2019

Thank you daniel,

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

Jesus