Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2006, 09:49 PM   PM User | #1
romalong
New Coder

 
Join Date: Jul 2003
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
romalong is an unknown quantity at this point
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!
romalong is offline   Reply With Quote
Old 01-11-2006, 01:19 AM   PM User | #2
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
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();"
Velox Letum is offline   Reply With Quote
Old 01-11-2006, 01:23 AM   PM User | #3
blu3t00th
New Coder

 
Join Date: Aug 2005
Posts: 46
Thanks: 3
Thanked 0 Times in 0 Posts
blu3t00th is an unknown quantity at this point
do while loops in while loops
blu3t00th is offline   Reply With Quote
Old 01-11-2006, 09:45 AM   PM User | #4
Rich Pedley
Regular Coder

 
Join Date: Sep 2005
Location: Liverpool
Posts: 226
Thanks: 0
Thanked 0 Times in 0 Posts
Rich Pedley is an unknown quantity at this point
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
Rich Pedley is offline   Reply With Quote
Old 01-11-2006, 02:01 PM   PM User | #5
romalong
New Coder

 
Join Date: Jul 2003
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
romalong is an unknown quantity at this point
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.
romalong is offline   Reply With Quote
Old 01-11-2006, 08:12 PM   PM User | #6
Rich Pedley
Regular Coder

 
Join Date: Sep 2005
Location: Liverpool
Posts: 226
Thanks: 0
Thanked 0 Times in 0 Posts
Rich Pedley is an unknown quantity at this point
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
Rich Pedley is offline   Reply With Quote
Old 01-11-2006, 08:32 PM   PM User | #7
Rich Pedley
Regular Coder

 
Join Date: Sep 2005
Location: Liverpool
Posts: 226
Thanks: 0
Thanked 0 Times in 0 Posts
Rich Pedley is an unknown quantity at this point
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
Rich Pedley is offline   Reply With Quote
Old 01-11-2006, 09:11 PM   PM User | #8
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:02 AM.


Advertisement
Log in to turn off these ads.