Reversed breadcrumb issue
My navigation table looks like this.
It based on a parent<->child scheme.
I'm using following code to generate breadcrumb.
function makeBreadcrumb($current, $lang, $db){
$q = $db->query("SELECT id, parent, $lang AS name FROM nav WHERE id = '$current'");
$row=$q->fetch_object();
echo "<li>";
开发者_运维问答 echo '<a href="?id=' . $row->id . '">' . $row->name . '</a>';
echo "</li>\n\n";
if($row->parent) makeBreadcrumb($row->parent, $lang, $db);
}
But getting reversed breadcrumb: it shows child>parent
instead of parent>child
.How to fix that?
The solution is very easy...you can simply put the recursive call before the echo
statment, like this:
function makeBreadcrumb($current, $lang, $db){
$q = $db->query("SELECT id, parent, $lang AS name FROM nav WHERE id = '$current'");
$row=$q->fetch_object();
if($row->parent) makeBreadcrumb($row->parent, $lang, $db);
echo "<li>";
echo '<a href="?id=' . $row->id . '">' . $row->name . '</a>';
echo "</li>\n\n";
}
But I raccomend you not to do a query for each node in the tree. It could be very resource-expenshive.
精彩评论