database tree php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
CreativeForce
Forum Newbie
Posts: 4
Joined: Wed Sep 15, 2004 7:24 am

database tree php

Post by CreativeForce »

i have this database which is fairly simple:

id id_numeric
1 0
2 1
3 1
4 0
5 3
6 3
7 5
8 0
9 7

i wish to print out this(a tree):
1 0
2 1
3 1
5 3
7 5
9 7
6 3
4 0
8 0

0 means that they are root. i'm not sure how to start. do i make them to arrays or do i loop. i don't know so i would really appreciate your help here. :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I can't see a particular pattern to the output? By a tree what exactly do you mean?

Mac
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

pattern is perhaps:

Code: Select all

1 0
    2 1
    3 1
        5 3
            7 5
                9 7
        6 3
4 0
8 0
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Aah - just looks like numbers when it's put in a straight line. So it's recursion that's needed - maybe this will help: http://www.sitepoint.com/article/hierar ... a-database

Mac
CreativeForce
Forum Newbie
Posts: 4
Joined: Wed Sep 15, 2004 7:24 am

Post by CreativeForce »

any other alternative to that. i'm thinking arrays. but i don't know how.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Did you have a go at the code in the SitePoint article - it had a bunch of code you could make a start on? Basically you're going to need a recursive function - one that calls itself - in order to generate the tree of parents and children.

Mac
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this may be of help too.
Post Reply