Welcome Guest, Not a member yet? Register   Sign In
Create a menu out of a parent-child relation table
#1

[eluser]yannyannyann[/eluser]
Hello,

I have to do a navigation menu for my site that has pages organised in parent / child style.

In my database I have a table that describes each line like this :

Code:
id_parent | id_child
--------------------
1         | 2
1         | 3
3         | 4
3         | 6
etc...


I php i get this array :

Code:
Array
(
    [0] => Array
        (
            [id_parent] => 1
            [id_child] => 2
        )

    [1] => Array
        (
            [id_parent] => 1
            [id_child] => 3
        )

    [2] => Array
        (
            [id_parent] => 3
            [id_child] => 4
        )

    [3] => Array
        (
            [id_parent] => 3
            [id_child] => 6
        )

etc ...

I would like to create a navigation menu out of this. How could I do so ?

Example of my wished nav menu :

Code:
1
-> 2
-> 3
    -> 4
    -> 6
-> 7
8
9
-> 12
    -> 11
    -> 10
13

THanks in advance for your help, cause I am a bit stuck here !
#2

[eluser]Kyle Johnson[/eluser]
I ran into a problem like this too, sadly there is no great way to get around it (that I've yet come across).

I did find this article (and saved it for later reference) because it provided two workable solutions. View it here.

The thing I didn't like about the first approach was the necessity for submitting numerous queries to the server every time a menu was displayed. (Basically one query is submitted for each menu and then each submenu).

Hopefully the tutorial / guide can help you out.
#3

[eluser]slowgary[/eluser]
There are a lot of threads about this already. Search for 'hierarchical' or 'hierarchical menu'. Here's a thread I started about the topic: http://ellislab.com/forums/viewthread/110050/

Also, you could eliminate the need for that table and just store the parent id of an element within the element's row. For instance:
Code:
menu table
=======================================================
|   item_id   |   item_parent_id   |   item_caption   |
-------------------------------------------------------
|   1         |   NULL             |    hello         |
|   2         |   1                |    yes sir       |
|   3         |   1                |    apples        |
-------------------------------------------------------

Would represent a menu like this:
Code:
hello
-> yes sir
-> apples




Theme © iAndrew 2016 - Forum software by © MyBB