Find latest record otherwise all others if statement

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

By Mikey - September 9, 2019

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, 2019 - edited: September 9, 2019

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

By daniel - September 9, 2019

Hi Zicky,

To do something like this, you can modify the foreach to provide the "key" of the current element which, in the case of these records, should be a numerical index starting with 0. This can then be used to check for the first item. It could look something like this:

<?php foreach ($digital_magazineRecords as $key => $record): ?>
<?php if ($key == 0): ?>

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

<?php else: ?>

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

<?php endif; ?>
<?php endforeach ?>

Note that this checks for the first item in the records array, which isn't necessarily the newest record, depending on what parameters are being used in getRecords().

Let me know if that suits your needs, or if you have any questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 9, 2019

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

By Mikey - September 9, 2019

Thanks Jerry!

'orderBy' => "createdDate DESC",

That was part of the solution I needed. 

By Mikey - September 9, 2019

Thanks Daniel and Jerry,

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

Thank you both!!!

Zick