List 3 records per line

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

By Jesus - April 24, 2019

Hi,

Because of my design,  I need to show 3 records per line, as my last record on each line needs to have a last_colum class. 

Does anyone knows how can I display 3 records per line, using a specific class every 3rd record?

Thank you in avance,

Jesus

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