homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Breadcrumbs - a recursive function
el_roboto




msg:3459413
 6:02 pm on Sep 24, 2007 (gmt 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

 

Habtom




msg:3459999
 6:25 am on Sep 25, 2007 (gmt 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?

el_roboto




msg:3460579
 6:26 pm on Sep 25, 2007 (gmt 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,'');

cameraman




msg:3460626
 7:05 pm on Sep 25, 2007 (gmt 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);
}

el_roboto




msg:3460658
 7:36 pm on Sep 25, 2007 (gmt 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

cameraman




msg:3460680
 7:59 pm on Sep 25, 2007 (gmt 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.

cameraman




msg:3460689
 8:06 pm on Sep 25, 2007 (gmt 0)

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

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

el_roboto




msg:3460736
 9:07 pm on Sep 25, 2007 (gmt 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

cameraman




msg:3461120
 6:25 am on Sep 26, 2007 (gmt 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.

el_roboto




msg:3461171
 8:01 am on Sep 26, 2007 (gmt 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved