1

Spent a few hours on this and need some expert help.

I have a table like this:

[id] [name] [parent_id]
1    fruits  0
2    orange  1
3    lemon   2
4    steak   0

When I go to lemon, I want the breadcrumb to be like:

Home > Fruits > Orange > Lemon

And lemon not to be a link but the rest to be a link.

Any suggestions?

The best I found is this but it makes everything into a link.

    function createPath($id, $category_tbl) {

        $s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
        $r = mysql_query($s);
        $row = mysql_fetch_array($r);

        if($row['parent_id'] == 0) {
            $name = $row['name'];
            return "<a href='index.php'>Admin</a> > <a href='index.php?folder_id=$id'>".$name."</a> > ";
        } else {
            $name = $row['name'];
            return createPath($row['parent_id'],$category_tbl). " <a href='index.php?folder_id=$id'>".$name."</a> >";
        }
    }

Answer below from Erwin gave me what I need to make it work.

function createPath($id, $category_tbl, $except = null) {
    $s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
    $r = mysql_query($s);
    $row = mysql_fetch_array($r);
    if($row['parent_id'] == 0) {
        $name = $row['name'];
        if(!empty($except) && $except == $row['id']) {
            return "<a href='index.php'>Admin</a> &raquo; ".$name."";
        }
        return "<a href='index.php'>Admin</a> &raquo; <a href='index.php?folder_id=$id'>".$name."</a> &raquo; ";
    } else {
        if(!empty($except) && $except == $row['id']) {
            $name = $row['name'];
            return createPath($row['parent_id'],$category_tbl, false). " $name";
        } 
        $name = $row['name'];
        return createPath($row['parent_id'],$category_tbl, false). " <a href='index.php?folder_id=$id'>".$name."</a> &raquo;";
    }
}
3
  • Are you having trouble with retrieving the data from the table or creating the html? Commented Oct 29, 2010 at 6:47
  • I am having trouble creating the HTML in PHP... how to make the last item in bread crumb not a link. Commented Oct 29, 2010 at 6:58
  • You know what I just realized... It's probably easier to use CSS Pseudo elements to do this than backend. Like css-tricks.com/triangle-breadcrumbs Commented Nov 11, 2010 at 19:03

3 Answers 3

4

add a third parameter, which will be the name of the link that supposedly will not be rendered into an a tag

function createPath($id, $category_tbl, $except = null) {

    $s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
    $r = mysql_query($s);
    $row = mysql_fetch_array($r);

    if($row['parent_id'] == 0) {
        $name = $row['name'];  
        return "<a href='index.php'>Admin</a> > <a href='index.php?folder_id=$id'>".$name."</a> > ";
    } else {
        $name = $row['name'];
        if(!empty($except) && $except == $name)
            return createPath($row['parent_id'],$category_tbl, $except)." ".$name;
        }
        return createPath($row['parent_id'],$category_tbl, $except). " <a href='index.php?folder_id=$id'>".$name."</a> >";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Didnt quite work the way it was but after a bit of tweaking, got it working. I put new code on original question. Thanks so much!
1

Doesn't that code get the results backwards Lemon > Orange > Fruits > Home? As joni suggested, I'd put the results in an array and then build the output string.

If you have other information in the database that you need, such as a url besides folder_id=$id you can store it as

$breadcrumb_items = array(
      0 => array( 'id' => '3', 
                  'title' => 'Lemon', 
                  'url' => 'LemonURL'
                ),
      1 => array( 'id' => '2', 
                  'title' => 'Orange', 
                  'url' => 'OrangeURL'
                ),
      2 => array( 'id' => '1', 
                  'title' => 'Fruit', 
                  'url' => 'FruitURL'
                ),
      3 => array( 'id' => '0', 
                  'title' => 'Home', 
                  'url' => 'HomeURL'
                )
);

Then call array_reverse to fix the array order, and build your html. Make sure to set a flag to prevent the last item from being made into a link.

$targetID = 3; //Lemon
foreach( $breadcrumb_items as $breadcrumb ){
...
   if( $breadcrumb['id'] != $targetID ){ //if the id does not match our target id
      //add link code
   }
...
}

1 Comment

Personally, I'd rather use an actual for loop and just have the special case be the last element.
0

Instead of outputting it just in time, create an array and buffer the results there. After the array's got filled by createPath() you can output the breadcrumb by a for loop that knows then which that the last element is (by count()) and can avoid to make a link out of it.

2 Comments

hmm.. how would i output it as an array?
something like ` $list[] = array('id'=>$id,'name'=>$name);` instead of return "<a href='index.php'>Admin</a> > <a href='index.php?folder_id=$id'>".$name."</a> > ";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.