Testing for last item in loop

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

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: [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

Jason/Collin, fantastic - thanks so much :)