1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP - Displaying Multiple Categories.

Discussion in 'HTML, Graphics & Programming' started by suarve, 14 Mar 2010.

  1. suarve

    Mobster

    Joined: 8 Oct 2005

    Posts: 4,190

    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
     
  2. markeh

    Gangster

    Joined: 7 May 2004

    Posts: 353

    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....
     
  3. suarve

    Mobster

    Joined: 8 Oct 2005

    Posts: 4,190

    Location: Midlands, UK

    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
     
  4. vaultingSlinky

    Mobster

    Joined: 2 Apr 2004

    Posts: 2,708

    Location: Aberdeen

    Edit: Nevermind, I'm being slow
     
  5. suarve

    Mobster

    Joined: 8 Oct 2005

    Posts: 4,190

    Location: Midlands, UK

    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
     
  6. suarve

    Mobster

    Joined: 8 Oct 2005

    Posts: 4,190

    Location: Midlands, UK

    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_IDsarray_merge($this->get_path($row['parent']), $parent_IDs);
            }

            return 
    $parent_IDs;
        } 

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