How to get all parents in a hierarchical category structure?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sallieann
Forum Newbie
Posts: 4
Joined: Fri Jan 30, 2009 2:03 am

How to get all parents in a hierarchical category structure?

Post by sallieann »

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;

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;
}
Currently it is only outputting;

Scotland - Glasgow

Can anybody explain why it isn't looping correctly? Any help greatly appreciated!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to get all parents in a hierarchical category structure?

Post by Benjamin »

In your query "id" is a reserved MySQL keyword so the query is failing. Place backticks around it.

Code: Select all

 
SELECT `id`, name, parent FROM dir_category
 
That is the first thing I noticed.
sallieann
Forum Newbie
Posts: 4
Joined: Fri Jan 30, 2009 2:03 am

Re: How to get all parents in a hierarchical category structure?

Post by sallieann »

Hi, thanks for your reply.

I've added the backticks as recommended and it's now only outputting the current category (ie, Scotland).
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to get all parents in a hierarchical category structure?

Post by Benjamin »

Have you checked your error log?
sallieann
Forum Newbie
Posts: 4
Joined: Fri Jan 30, 2009 2:03 am

Re: How to get all parents in a hierarchical category structure?

Post by sallieann »

eek... I don't think I have an error log. How do I go about creating one?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to get all parents in a hierarchical category structure?

Post by Benjamin »

Code: Select all

 
 error_reporting(E_ALL);
 ini_set("display_errors", 1);
 
Just put that above your code on the page that has problems. Ideally you are developing on a development server so you would then be able to enable error reporting via php.ini.
sallieann
Forum Newbie
Posts: 4
Joined: Fri Jan 30, 2009 2:03 am

Re: How to get all parents in a hierarchical category structure?

Post by sallieann »

I'm getting no output from the error reporting. Do you think the actual function itself looks correct? I wasn't sure if I'd need another query within the "foreach" loop.
Post Reply