Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a tree from a resultset

Status
Not open for further replies.
Sep 5, 2006
37
US
Hi,

I need to create a tree structure from a resultset and show them into my JSP. Here is my table sql (grup).

Code:
CREATE TABLE GRUP (
ID_GRUP int(11) NOT NULL default '0',
ID_GRUP_PARENT int(11) NOT NULL default '0',
ORDER_BY int(11) NOT NULL default '1',
DESCRP varchar(100) default NULL,
PRIMARY KEY ('ID_GRUP'),
)

What I really need is a recursive method to search for all the children of each row and build a tree heirarchy. e.g. the data may look like


Code:
ID_GRUP   ID_GRUP_PARENT      ORDER_BY    DESCRP
1               0                 1        It itself is the parent
2               1                 1        It's Parent is ID_GRUP=1
3               0                 2        It itself is the parent
4               2                 1        It's parent is ID_GRUP=2
5               1                 2        It's Parent is ID_GRUP=1
6               4                 1        It's Parent is ID_GRUP=4
7               2                 2        It's parent is ID_GRUP=2
8               2                 3        It's parent is ID_GRUP=2

So looking at the top data my final tree structure should look like

Code:
1
  2
    4
      6
    7
    8
  5
3
and so on. ORDER_BY show the order in which each element should display in the heirarchy. I am really struggling to find a way on how to have the heirarchy built and really need some help.

Thanks


Here is the SQL i have to het the data

Code:
SELECT ID_GRUP, ID_GRUP_PARENT, DESCRP FROM GRUP ORDER BY ORDER_BY.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top