If last (based on order, not num)

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

By rjbathgate - April 1, 2009

Hey,

I need to write an IF statement for is the record is the last one in the (multi record) table.

However, something based on the record num (i.e. IF num = totalRecords) doesn't work as I need it based on the table order rather than num.

For example
2: Hi
3: Hello
4: Bye
1: Goodbye

I need Goodbye to be recognised as the last, even though it's num = 1, it is last as per order.

Note, the table is ordered by another field, DATE (which is date format), not DragSortOrder if that matters.

Cheers in advance
Rob

Re: [rjbathgate] If last (based on order, not num)

By Dave - April 1, 2009

Hi Rob,

Do you need to load all the records or could you just use these options to load the last one?

'orderBy' => 'date',
'limit' => 1,

That will load one record, and you can make it the last one by changing the orderBy (date, or date DESC). If you need to load them all and get the last one you could do something like this:

<?php
$lastRecord = array();
foreach ($records as $record) { $lastRecord = $record;}
?>


Let me know if either of those would work for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] If last (based on order, not num)

By rjbathgate - April 1, 2009

Hi Dave,

Yeah, need to display all data, so a limit 1 won't work.

How do I implement this:

<?php
$lastRecord = array();
foreach ($records as $record) { $lastRecord = $record;}
?>


This is what I have at moment:
<?php foreach ($tableRecords as $record): ?>

<?php if (LAST): ?>
IF LAST CONTENT

<?php else: ?>
ELSE CONTENT

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


Cheers Dave

Re: [ross] If last (based on order, not num)

By rjbathgate - April 2, 2009

Hey,

<?php if ($record == end($tableRecords )): ?>

worked a treat.

Thanks