PHP - Displaying Multiple Categories.

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Having a little trouble here :(

I read a bit of tutorial - http://www.codeassembly.com/How-to-...pandable-categories-using-php-and-javascript/ - showing how to display categery levels for the following category structure :

CatID
Cat Name
Cat Parent ID

Where a cat parent id of 0 would mean a top level category.

Now this all works fine when I want to display the category tree for ALL category, but I'm really stuck as to how I can display one category trial for a given cat id

E.g. Top Cat Name > Sub cat 1 > Sub Cat 2


Any help muchly appreciated.

Thanks
 
Have a function that loops through all the categories with the parent ID of the the ID passed to it, then call the function from itself when a matching category id is found passing to it the id of the sub category, adding the id/name onto a string or maybe an array. It shouldn't get stuck in an infinite loop I guess if it only calls itself when a parent id matching the value passed to the function is found.

That's the theory I'd use anyway, if it makes any sense....
 
That's what the function in the link does.

However, I want to say display a breadcrumb of a specific cat id E.g. Top Cat > Sub cat
 
Anyone? :(

I've hit a brick wall at the mo. I need to display one node/branch for a given cat id E.g Home > Top Cat > Sub Cat > sub Sub Cat etc.

...for a parent / child datbase structure:

catID
catName
parent
 
Woohoo, in case anyoner is interested, here's the solution I came up with!

After much googling I randomly came across a solution which I changed a bit. It returns an array of category IDs - representing the path to a particular category ID.

As the fuction uses recusion, I'll only have a max of 7 levels too (beofre anyone points out that recusion is slow) :)

PHP:
function doBreadcrumb($catID) {
        $row = DB::getInstance->query("SELECT parent FROM categories WHERE ID = '$catID'")->fetch();

        $parent_IDs = array();
        if (!$row['parent'] == '') {
            
            $parent_IDs[] = $row['parent'];
            $parent_IDs= array_merge($this->get_path($row['parent']), $parent_IDs);
        }

        return $parent_IDs;
    }

I'm actually chuffed to bits - how sad :)
 
Back
Top Bottom