6 posts by 3 authors in: Forums > CMS Builder
Last Post: September 9   (RSS)

Howdy folks,

I'm trying to find the latest record in a foreach loop and show additional information for the newest record, and all other older records I only wish to show their titles. But I'm struggling to figure this out. Any suggestions on how to accomplish this are much appreciated. 

  // load records from 'digital_magazine'
  list($digital_magazineRecords, $digital_magazineMetaData) = getRecords(array(
    'tableName'   => 'digital_magazine',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

<?php foreach ($digital_magazineRecords as $record): ?>

// find only the latest record and show the title and cover image.

   <h1><?php echo htmlencode($record['title']) ?></h1>
   <?php foreach ($record['cover_image'] as $index => $upload): ?>
      <img src="<?php echo htmlencode($upload['thumbUrlPath2']) ?>"/>
   <?php endforeach ?>

// all other records show only the record title.

<h4><?php echo htmlencode($record['title']) ?></h4>

<?php endforeach ?>

Thanks, Zick

By gkornbluth - September 9 - edited: September 9

Hi Zick,

Probably not the most elegant approach, but if you sort by either createdDate or updatedDate (which ever makes more sense) and limit the load to one record, something like:

list($digital_magazineRecords, $digital_magazineMetaData) = getRecords(array(
'tableName' => 'digital_magazine',
'allowSearch' => '0',
'orderBy' => "createdDate DESC",
'limit' => '1',
'loadUploads' => true,
'allowSearch' => false,
));

That should give you the newest record and you could output any information about that record you wanted to.

You could also create a variable from the record number of that newest record, like $newestRecNum

You could then do a new load records call without the limit, sorting the records the way you want to.

If you create another record number variable like $allRecNums within the second foreach loop, you could compare the 2 variables and only echo the title of those records where there's no match to the $newestRecNum variable.

Hope that gives you some ideas.

Good luck.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

I knew my approach wasn't particularly elegant...

Thanks Daniel

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Thanks Jerry!

'orderBy' => "createdDate DESC",

That was part of the solution I needed. 

Thanks Daniel and Jerry,

I applied both of your suggestions and everything is working great now!

Thank you both!!!

Zick