Testing for last item in loop

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

By theclicklab - December 4, 2011

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 Collin - December 5, 2011 - edited: December 5, 2011

What I often do for this is use the 'implode' function:

<?php

$menuItems = array();
foreach ($pagesRecords as $record){
$menuItems[] = "<a href=" . $record['_link'] . "> " . $record['name'] . "</a>"
}

$menu = implode(' | ', $menuItems);

echo $menu;
?>


http://php.net/manual/en/function.implode.php

Re: [Collin] Testing for last item in loop

By theclicklab - December 5, 2011

Jason/Collin, fantastic - thanks so much :)