Welcome to WebmasterWorld Guest from 54.198.196.102

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Breadcrumbs - a recursive function

     
6:02 pm on Sep 24, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


////////////////////////////////////////////////////
function OoohCrumbs($page,$string){
$page = DbConnector::makeDbSafe($page);
$getNav = DbConnector::query("SELECT `ID`, `NAME`, `PARENT`, FROM rcms_pages WHERE `ID` = '".$page."' LIMIT 1;");
$gotNav = DbConnector::fetchArray($getNav);
$crumbs = $string.$gotNav['URL'].' > ';
$parent = $gotNav['PARENT'];
if ($parent!= 'nav') {$this->PageURL($parent,$crumbs);}
else if ($parent == 'nav') {return $URL;}
}
////////////////////////////////////////////////////

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

6:25 am on Sept 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 15, 2004
posts:1867
votes: 0


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);

Is this outputing anything?

If the function is accessible, you should print it somehow.

Is $URL assigned anywhere?

6:26 pm on Sept 25, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


Sorry I wrote the function out with some mistakes there as I was changing variable names to make it easier to follow - my bad.

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,'');

7:05 pm on Sept 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


You're altering the flow when you change the echo to return. WIth the echo, execution then continues with the line below it, whereas returning from there doesn't.
Try this:
// if ($parent == 'nav') { echo $Crumbs; }
if ($parent!= 'nav') { $this->OohCrumbs($parent,$Crumbs); }
return($Crumbs);
}
7:36 pm on Sept 25, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


ahhhhhhhhh - so even if the return is within an if clause it stops the function regardless?

I just tried your suggestion - it does return a value but only the current page of the trail

ie:

where it should be fruit > green > apple

I get apple

7:59 pm on Sept 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Nono, it doesn't stop when the if is false.

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.

8:06 pm on Sept 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Try changing
function OohCrumbs($page,$string){

to
function OohCrumbs($page,&$string){

9:07 pm on Sept 25, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


the problem seems to be with returning a variable from

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

6:25 am on Sept 26, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Ok, I finally see what's going on - you almost had it there in your last post.
When you return $Crumbs, where is it going? Nowhere - you're pitching it, but not catching it.

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.

8:01 am on Sept 26, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


That's the ticket!

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!