Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fold out categories hierarchy from DB

    hi!

    i'm stuck with hierarchial fold out data display taken from database.
    say i've got main menu elements:

    Code:
    home
    audio
    foo
    if one category is pressed and it has children, these children should appear.
    audio is accessed:

    Code:
    home
    audio
    speakers headphones
    foo
    speakers is accessed:

    Code:
    home
    audio
    speakers
    2.1 4.1
    headphones
    foo
    foo is accessed and audio->speakers are closed:
    Code:
    home
    audio
    
    foo
    
    something
    my db layout:

    Code:
    +---------------+------------------+------+-----+---------+----------------+
    | Field         | Type             | Null | Key | Default | Extra          |
    +---------------+------------------+------+-----+---------+----------------+
    | cat_id        | int(10) unsigned |      | PRI | NULL    | auto_increment |
    | parent_cat_id | int(100)         | YES  | MUL | NULL    |                |
    | cat_name      | varchar(150)     |      | UNI |         |                |
    | cat_order     | int(11)          |      |     | 0       |                |
    +---------------+------------------+------+-----+---------+----------------+
    finally, my php code:

    PHP Code:
    function select$level 0$pre '' )
        {
            
    $SQL 'SELECT
                            * 
                        FROM
                            ' 
    CATEGORIES_TABLE '
                        WHERE
                            parent_cat_id = ' 
    $this->parent ' ORDER BY cat_id'
            
            
    $result mysql_query$SQL ) or die( mysql_errno() );
            
            
    $i 0;
            
            while( 
    $row mysql_fetch_assoc$result ) )
            {
            
                
    $this->cats .= str_repeat'&nbsp;&nbsp;'$level ) . "<a href=\"" $_SERVER['PHP_SELF'] . "?cat_id=" 
                                
    $row['cat_id'] . "\">"
                                    
    $row['cat_name'] . "</a><br>" "\n";        

                
    $this->parent $row['cat_id'];
            
                if( 
    $_REQUEST['cat_id'] == $row['cat_id'] )
                {
                    
    $this->select$level 1$pre $row['parent_cat_id'] );                
                }

    //before i fetched the entire structure        
    //$this->select( $level + 1, $pre = $row['parent_cat_id'] );
          
                
    $i++;
            }
        } 
    initially, it displays all the main level cats. then, if any category is hit and one's got children they're displayed too, this condition is responsible of it: 'if( $_REQUEST['cat_id'] == $row['cat_id'] )'. but if this child cat is accessed, condition is evaluated to false b/c $_REQUEST['cat_id'] != $row['cat_id'] and it doesn't fold out anylonger. any ideas on how to implement such behaviour?

    thanx!

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not too sure about the subcat thing, but marek posted this interesting link about hierarchal tree categories or something like that which might help.

    MPTT
    "$question = ( to() ) ? be() : ~be();"

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    3
    Thanked 0 Times in 0 Posts
    do while loops in while loops

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bah

    I couldn't figure out how to do this either so if you ever find a solution let me know.

    You need to recursively check for parent_cat_id until it equals your top level...

    I still don't understand the 'right id / left id' method enough to incorporate it anywhere *sigh*
    my mind is on a permanent tangent

  • #5
    New Coder
    Join Date
    Jul 2003
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Velox Letum
    I'm not too sure about the subcat thing, but marek posted this interesting link about hierarchal tree categories or something like that which might help.

    MPTT
    thanx for the link, but i'm using adjacency list model, not preorder traversal algorythm.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just re-reading and looks like I am looking for the same thing.

    e.g.
    a top level menu comprised of:

    Public
    Private
    Old

    choosing any one of them then changes the menu to something like:

    Public
    Private
    -Parts
    -Service
    Old

    though obviously we have to consider:
    Public
    Private
    -Parts
    --Meat
    --Veg
    -Service
    Old

    etc.

    Although this tutorial has been useful, I still can't see how to adapt it for my needs. It appears that it can show the entire tree, or just a portion of it, but not what I want!. Maybe I'm missing something?
    my mind is on a permanent tangent

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    romalong,
    http://www.chipchapin.com/WebTools/MenuTools/
    might have something useful, working my way through at the moment
    my mind is on a permanent tangent

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I'd suggest adding a field that would store the path to the subcategory (just name & id of all it's parents).
    It's the simplest way to have the path "cached" so that you can build the links from it.
    Generating the field is simple. If it's empty it copies it's parents path and adds it's parents data (recursibve if the parent's data is empty aswell).
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •