Forum Moderators: coopster
I've written the function above to generate a breadcrumb trail when a given pages ID is passed into it. ($string is only important with the recursive part of the function)
In my DB each page has, amongst other things, an ID, NAME, and a PARENT. If the page is a top tier navigation item PARENT is 'nav', otherwise PARENT = the ID of the parent page.
In my eyes the function above should compile a breadcrumb string such as (ignoring the brackets):
Articles(ID:2,PARENT:nav) > Football(ID:7,PARENT:2) > December(ID:33,PARENT:7)
and return this once the recursive function reaches the top level, however I'm getting no return from the function at all.
Can anyone see any blatent reason this wouldn't be working, I've been playing around with it for the best part of today with no success
if ($parent!= 'nav') {$this->PageURL($parent,$crumbs);}
else if ($parent == 'nav') {return $URL;}
}
I don't know if this helps but, that doesn't make much sense to me.
$this->PageURL($parent,$crumbs);
If the function is accessible, you should print it somehow.
Is $URL assigned anywhere?
I've continued playing around with it since then, my current version is
///////////////////////////////////////////////////////////
function OohCrumbs($page,$string){
$page = DbConnector::makeDbSafe($page);
$getNav = DbConnector::query("SELECT `ID`, `NAME`, `PARENT`, `URL` FROM rcms_pages WHERE `ID` = '".$page."' LIMIT 1;");
$gotNav = DbConnector::fetchArray($getNav);
$Crumbs = $gotNav['NAME'].' > '.$string;
$parent = $gotNav['PARENT'];
if ($parent == 'nav') { echo $Crumbs; }
if ($parent!= 'nav') { $this->OohCrumbs($parent,$Crumbs); }
}
///////////////////////////////////////////////////////////
This function echos the breadcrumb trail correctly, however, I would rather return the value of $Crumbs than echo it, and this is when I fail to see any output
ie: If I change the above line to
if ($parent == 'nav') { return $Crumbs; }
then below returns nothing
print $BC->OohCrumbs(4,'');
if ($parent == 'nav') { echo $Crumbs; }
if ($parent!= 'nav') { $this->OohCrumbs($parent,$Crumbs); }
if ($parent == 'nav') { return; }
if ($parent!= 'nav') { $this->OohCrumbs($parent,$Crumbs); }
In the first one, if the parent is nav, the value of crumbs will be echoed and the next statement will be evaluated.
In the second one, if the parent is nav, the function exits and the next statement is not evaluated.
if ($parent == 'nav') { return $Crumbs; }
if I
if ($parent == 'nav') { echo $Crumbs; }
then I can see the results ok when I'm testing, I can't understand why its not returning the value, I've even tested as
if ($parent == 'nav') { $Crumbs = 'do something!'; return $Crumbs; }
still nothing.
If I return $Crumbs just before the close of the function I do receive a value, but it's only for the current item, it doesn't do the recursive function
AAAAAAAAAAAAAAAAAAAAAAAAAARRRRRRRRRR
When you iteratively call the function with
if ($parent!= 'nav') { $this->OohCrumbs($parent,$Crumbs); }
you're not doing anything with a return value.
You need to do something like:
if ($parent!= 'nav') { $crumbs = $this->OohCrumbs($parent,$Crumbs); }
return($crumbs);
}
so that it gets bubbled back up to the first call. And you don't need the line:
if ($parent == 'nav') { echo $Crumbs; }
at all.
Ok to close the topic, heres the full working function
/////////////////////////////////////////
function OohCrumbs($page,$string){
$page = DbConnector::makeDbSafe($page);
$getNav = DbConnector::query("SELECT `ID`, `PARENT`, `URL` FROM rcms_pages WHERE `ID` = '".$page."' LIMIT 1;");
$gotNav = DbConnector::fetchArray($getNav);
$crumbs = $gotNav['URL'].'/'.$string;
$parent = $gotNav['PARENT'];
if ($parent!= 'nav') { $crumbs = $this->OohCrumbs($parent,$crumbs); }
return($crumbs);
}
/////////////////////////////////////////
Thanks very much for your help on that one!