Testing for last item in loop

4 posts by 3 authors in: Forums > CMS Builder
Last Post: December 5, 2011   (RSS)

Hi there,

I need to test for the last item in a navbar so i can remove the separator " | ", could not find it in the forum.

Here is the code:

<?php
// load records
list($pagesRecords, $pagesMetaData) = getRecords(array(
'tableName' => 'pages',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => 'footer_nav ="1"',
'useSeoUrls' => true,
));
?>


<?php foreach ($pagesRecords as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['name'] ?> </a> |
<?php endforeach ?>


Many thanks

Re: [theclicklab] Testing for last item in loop

By Jason - December 5, 2011

Hi Jan,

One approach would be to put all your menu links into a string and then use the rtrim() function to remove the last | from the string.

For example:

<?php $menu = ""; ?>
<?php foreach ($pagesRecords as $record): ?>
<?php $menu .= " <a href=\"". $record['_link'] ."\">". $record['name']." </a> |"; ?>
<?php endforeach ?>

<?php $menu = rtrim($menu, "|"); ?>
<?php echo $menu; ?>



Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Collin] Testing for last item in loop

Jason/Collin, fantastic - thanks so much :)