RE: Generating view of tree?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What's your table structure look like? I've done this in a couple projects.
I use a parent child relationship. Usually I make it so you can expand and
collapse the tree by clicking on a plus or minus, the script I've attached
doesn't have this feature for simplicity sake.

parent  child  description
1       0      Category
2       1        Sub-category
3       2          Sub-sub-category
4       1        Another sub-category
5       0      Category
6       5        Sub-category

etc...

Create a recursive function (a function that calls itself) that starts where
the child = 0, then looks for child = parent. Keeping track of the indenting
is tricky, but not impossible. Since it is recursive, it probably does use a
lot of queries, so don't know if it's what you want or not.

The script I've included works for a table named "structure" with the
following fields:

structure
-------------------------------------
s_parent int not null auto_increment, primary key (s_parent)
s_child  int
s_desc   varchar(255)

I tested the script on this table just now, after taking out a lot of junk
that was doing other things (adding graphics, keeping track of expansion,
etc).

I don't warrent or support this script in any way shape or form. To the best
of my knowledge it works as described above, and you can use it however you
like, but like I said, it's just for use as an example. Oh, and if you use
it, include the comment at the top please. :)

Thanks.


-----Original Message-----
From: Blain [mailto:Blain@gmx.ch]
Sent: Wednesday, March 26, 2003 9:35 PM
To: Leif K-Brooks; php-db@lists.php.net
Subject: AW:  Generating view of tree?


If you can sort it and give all entrys a
indent number its easy to read the tree out.

-----Ursprungliche Nachricht-----
Von: Leif K-Brooks [mailto:eurleif@buyer-brokerage.com]
Gesendet: Donnerstag, 27. Marz 2003 03:19
An: php-db@lists.php.net
Betreff:  Generating view of tree?


I have a table with a tree.  Thing is, I need to generate a view of it like:
Category
    Sub-category
        Sub-sub-category
    Another sub-category
Another category
  Sub-category

Any way to do this, without using a huge number of queries?  Using MySQL.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux