Recurse properly
I have a few navigation-related features that I would like to have without depth restrictions. They generate CSS menus, breadcrumbs, etc.
I am focused on how I will make the functions by following each path to depth or root without an explicit loop.
Below is a typical example where I want to get the topmost parent of a page. The topmost value will have a value of 0 in the field parent
.
Here's an explicit loopback version:
function topPg() {
$p = $this->retrieve("id = '$this->parent'");
if ($p->parent != 0) {
$gp = $this->retrieve("id = '$p->parent'");
if ($gp->parent != 0) {
$ggp = $this->retrieve("id = '$gp->parent'");
if ($ggp->parent != 0) {
$gggp = $this->retrieve("id = '$ggp->parent'");
// ad naseum
} else {
return $ggp;
}
} else {
return $gp;
}
} else {
return $p;
}
} // func
Does anyone have any advice or similar code or link to tute to help point the way?
+1
a source to share