Posted by Phil Latio on 02/02/07 20:43
> It's most commonly called "breadcrumbing" (from the Hansel & Gretel fairy
> tale).
Thanks. Popped that into Google and came up with whole load of people asking
the same question as me. Surprised I didn't find any tutorials on this
subject.
> <?php
> function makeBreadcrumbs($link_id) {
> // set up a counter
> $counter=1;
> // get the name for this link_id
> $query="SELECT link_name, category_id FROM link WHERE link_id=$link_id";
> $result=mysql_query($query);
> $row=mysql_fetch_array($result);
> // put it as the first row in an array
> $bread_crumb_array=array();
> $bread_crumb_array[$count]=$row("link_name");
> // get the parent
> $category_id=$row("category_id");
> // now climb the tree if not already at the top
> while($category_id!=0) { // careful of errors here !!!
> // this time get the parent's name
> $query="SELECT link_name, category_id FROM link WHERE link_id=
> $category_id";
> $result=mysql_query($query);
> $row=mysql_fetch_array($result);
> // increment the counter
> $counter++;
> // put it in the array
> $bread_crumb_array[$counter]=$row("link_name");
> // get the next parent
> $category_id=$row("category_id");
> }
> // now you have an array of names to the top in reverse number order
> // get the last one first
> $breadCrumbString=$bread_crumb_array[$counter];
> // work back through the array
> while($counter>0) {
> $breadCrumbString.=" >> ".$bread_crumb_array[$counter];
> // decrement the counter
> $counter--;
> }
> return $breadCrumbString;
> }
> // test using the example
> $link_id=3;
> echo makeBreadcrumbs(link_id);
> ?>
Thanks for this code. It will assist me greatly.
I will actually post a working solution when I have one so everyone can
share it.
Cheers
Phil
[Back to original message]
|