PHP array question

larry98765

Registered
Hi All,

I've set up an array ($links) representing a list of links, and would like to loop over the list, create the links, and when I hit the current page ($page)
find out what the prev() and next() items are in the array so I can create previous and next links.

Problem: When I'm on the first page ("brochures") I don't get a next link, even though on the last page ("stationery") I DO get a previous link.

Can someone help my find the error of my ways?

PHP:
#my array
$links=array(
'brochures' => 'Brochures & collateral',
'webdesign' => 'Web design & site management',
'logodesign' => 'Logo design & corp. identity',
'stationery' => 'Stationery systems'
);

PHP:
function ls_linklist($links,$page) {

global $nextpage;
global $prevpage;

foreach($links as $link=>$description) {
	if ($page==$link) {
		$linklist .= "<li class=\"current\"><a href=\"$self?page=$link\">$description</a></li>\n";
		
		#do the cha-cha-cha!
		if (prev($links)) {
			$prevpage=key($links);
			next($links); #return to the current position
		}
		
		if (next($links)) {
			$nextpage=key($links);
			prev($links); #return to the current position
		}
		
	} else {
		$linklist .= "<li><a href=\"$self?page=$link\">$description</a></li>\n";
	}
next($links); #advance
}

echo $linklist;
}
 
Apparantly php doesn't like it when you use prev() when at the front of an array or next() when at the end. the pointer gets lost in limbo or something.

Try this inside your loop:
PHP:
if( prev($links) === false )
   reset($links);
else
{
   $prevpage = key($links);
   next($links);
}
if( next($links) === false )
   end($links);
else
{
   $nextpage = key($links);
   prev($links);
}
 
Back
Top