How to get all parents in a hierarchical category structure?
Posted: Fri Jan 30, 2009 2:34 am
Hi,
I'm trying to build a breadcrumb trail which displays all parents to any particular category. For example;
Fieldwork Suppliers - United Kingdom - Scotland - Glasgow
Each category is stored in the database with a name, id and parent.
I'm using the following to call the function;
$pageTitle = getCatPageTitle($cat->name, $cat->parent, $catid);
and the function is as follows;
Currently it is only outputting;
Scotland - Glasgow
Can anybody explain why it isn't looping correctly? Any help greatly appreciated!
I'm trying to build a breadcrumb trail which displays all parents to any particular category. For example;
Fieldwork Suppliers - United Kingdom - Scotland - Glasgow
Each category is stored in the database with a name, id and parent.
I'm using the following to call the function;
$pageTitle = getCatPageTitle($cat->name, $cat->parent, $catid);
and the function is as follows;
Code: Select all
function getCatPageTitle($catName, $parentId, $catId) {
global $database;
$sql = "SELECT id, name, parent from dir_category";
$database->SetQuery($sql);
$rows = $database->loadObjectList();
$titleArray[0] = $catName;
foreach ($rows as $row){
if($row->id==$parentId) {
$titleArray[] = $row->name." - ";
$parentId = $row->id;
}
}
krsort($titleArray);
reset($titleArray);
$title = "";
foreach($titleArray as $key => $value){
$title.= $value;
}
return $title;
}Scotland - Glasgow
Can anybody explain why it isn't looping correctly? Any help greatly appreciated!