Tree Nav with PHP/MySQL

Hi all!
I’ve been looking on the web and I don’t see anything that looks a) reasonably simple b) will work. I was hoping one of the superheros here could help out.

What I need to do is create a nav menu using PHP/Mysql. I’m already doing a Gallery/category structure which looks like this:


I want to make it dynamic enough to allow more levels:
Level 1
– Level 2
----Level 2a
------Level 2a2

Obviously I don’t recommend that many levels…but I figure with an n-tier script the actual number wouldn’t matter.

My thought process was that each category in a level would have the following attributes:
Parent_id = ‘1’, Child_id = ‘1’,
Parent_id = ‘1’, Child_id = ‘2’,
Parent_id = ‘2’, Child_id= ‘1’,
Parent_id = ‘2’, Child_id=‘3’,

Where the parent id of the next level would be the same id of the child level above it. I was thinking I could do a sort on mysql to order by parent_id, child_id. That way if I did a select on the whole table, it should be in order to just run out in the array.

Would this work? Anyone have any better ideas?

Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development

I think you will need 4 fields: id, parent, and order, value.

A tree is basically a list of lists of lists… so you need a way to sort the list (order) and each list needs a link to the parent list (parent) and its own id.


I. A. B. i. ii. iii. C. II.You have

id=1 parent=0 order=1 value=I
id=2 parent=0 order=2 value=II
id=3 parent=1 order=1 value=A
id=4 parent=1 order=2 value=B
id=5 parent=1 order=3 value=C
id=6 parent=4 order=1 value=i
id=7 parent=4 order=2 value=ii
id=8 parent=4 order=3 value=iii

Granted you could do the same with 3 fields if you keep the lists sorted, eg

id=1 level=0 value=I
id=2 level=1 value=A
id=3 level=1 value=B
id=5 level=2 value=i
id=6 level=2 value=ii
id=7 level=2 value=iii
id=8 level=1 value=C

:cool: -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7