开发者

PHP recursive function to get breadcrumb

开发者 https://www.devze.com 2023-01-22 17:52 出处:网络
Spent a few hours on this and need some expert help. I have a table like this: [id] [name] [parent_id] 1fruits0

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 开发者_Go百科> 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;";
    }
}


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> >";
    }
}


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
   }
...
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消