Page 1 of 1

Navigation function has bugs when invoked

Posted: Mon Mar 13, 2006 4:47 am
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!

Posted: Mon Mar 13, 2006 11:34 am
by feyd

Code: Select all

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