Navigation function has bugs when invoked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Navigation function has bugs when invoked

Post by mattcooper »

Hi,

Having spent a while trying to figure out a way to fix this, I now must turn to the expertise of those in this forum once more! Last time for a while, I hope...

I have written (with the help of one of the members of this forum - thanks again Feyd!) a relatively simple function to provide navigation between closely related pages. Here is the code:

Code: Select all

foreach(glob('includes' . DIRECTORY_SEPARATOR . basename($_GET['pagename']) . '*.php') as $file) 
{  


$num=$file;

$stripped=substr($num, 0, -4);
//echo "$stripped\n";
$pagename=substr($_GET['pagename'];
if(eregi($pagename,$stripped)){
$start=strlen($stripped)-2;
$num=substr($stripped, -1, $start);

$numarray=array($num);
	sort($numarray);
	foreach ($numarray as $num){
	if(is_numeric($num)){
	echo "<a href=\"$PHP_SELF?pagename={$pagename}_$num\">".$num."</a> ";
   }
   else{
   echo "<a href=\"$PHP_SELF?pagename=$pagename\">1</a> ";
   }
   }

}
}
It works well on the first page, but when you follow a link the "pagename" then has a number, which causes problems with the eregi(). I have tried many combinations of strlen() and sbstr() for the variables, but am having no luck. Maybe it's just my maths but I'm drawing a blank here!

The end result should be "1 2 3" or similar on each page viewed. At present, "1 2 3" appears only on the first page, and this is because my first pages, by default, do not have a "_#" extension. By adding one (i.e in the new pagename, *_2) the problems begin and the only link then displayed is one to the page I'm actually on!

Can somebody get me out of this pit?

Thanks in advance!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$pagename = preg_replace('#_\d+$#', '', $pagename);
Post Reply