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> » ".$name."";
}
return "<a href='index.php'>Admin</a> » <a href='index.php?folder_id=$id'>".$name."</a> » ";
} 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> »";
}
}