Hi,

Rather than writing a recursive function I'd suggest that you create a menu class and store each record as an object of the class. The menu class will have variables of id, parent, title, children, and tree. The children will be initialised to 0 and tree will be initialised to 1. You will need to write a function to find any menu object given the menu's ID, should be easy if you store each object in an array (keyed on the ID)

1) Read in a record and create a menu object
2) Get the parent object, using the parent variable and the find method
3) Increment the children variable in the parent menu object by one
4) Set the tree variable in the new menu object to the tree object in the parent object plus the value in the children object.


Now add method to display the menu items and your problem should be solved.

Gavin Amm wrote:

Hi guys,

I need to generate a menu based on the content structure of my database.

The data is structured using an 'id' & 'parent' relationship, for eg:

id

parent

title

1

0

Home Page

6

1

My Page 2

9

6

My Page 3

15

9

My Page 4

21

6

My Page 5

22

9

My Page 6

23

22

My Page 7

24

22

My Page 8

25

1

My Page 9

28

25

My Page 10

I need to create a recursive loop that will create the menu. The text in
the brackets is what I'm trying to achieve, the rest is just for
aesthetics & ease of reading.
(The children of the row id=1 are the top of the menu tree, ie they will
be the menu categories at the top of the web page to display the menu
when the mouse hovers over it.)
(Each menu item, being a menu, will have a href to it's id number.)

My Page 2 (Menu_1)
   My Page 3 (Menu_1_1)
       My Page 4 (Menu_1_1_1)
       My Page 6 (Menu_1_1_2)
           My Page 7 (Menu_1_1_2_1)
           My Page 8 (Menu_1_1_2_2)
   My Page 5 (Menu_1_2)
My Page 9 (Menu_2)
   My Page 10 (Menu_2_1)

I just can't for the life of me get the numbering to work correctly in
the recursion.
I seem to get things like (1_1_2_1_2_3_1_2_3_4) which I can see the
pattern in, but it's obviously not what I'm after..

Appreciate your help.
Thanks, Gav.





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



Reply via email to