Page 1 of 1

Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 7:02 am
by frank_mark
Hi all,
I'm still wrapping my head around PHP and enjoying learning the language.

I have reached a part that challenges my intelligence (or lack thereof).
I would like to generate a 3 level drop down menu with the information stored in the following array.
The following array is as follows:

Code: Select all

$items = array(
                            array('id' => '1', 'pid' => '0'),
                            array('id' => '2', 'pid' => '0'),
                            array('id' => '3', 'pid' => '0'),
                            array('id' => '4', 'pid' => '1'),
                            array('id' => '5', 'pid' => '1'),
                            array('id' => '6', 'pid' => '1'),
                            array('id' => '7', 'pid' => '2'),
                            array('id' => '8', 'pid' => '4')
                            );
 
where 'id' indicates its unique id and 'pid' (parent id) indicates the parent of the element. So item with 'id' of 8 will be a sub-item of item 4 that is in turn a sub-item of item 1.

I would like to be able to create a series of nested ul and li items to then style with css.

I've been torturing my little brain with a series of pathetic recursive functions... all they can achieve is an infinite loop of hopelessness.

I would really love it if someone could explain how I should go about achieving this.

Many thanks in advance,
Frank

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 7:11 am
by mattpointblank
Where in your code do you define the relationship between level 2 and level 1? There doesn't seem to be a link to the grandparent level.

EDIT: I think I follow now, you're saying that the parent item is defined in the same level of the array as the child. It might simplify things to make it into 3 levels, not 2?

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 7:18 am
by frank_mark
Hi mattpointblank,
thanks for your lightning fast reply.

So you say that I should restructure it as:

Code: Select all

# $items = array(
                             array('id' => '1', 'pid' => '0'),
                             array('id' => '2', 'pid' => '0'),
                             array('id' => '3', 'pid' => '0'),
                             array('id' => '4', 'pid' => '1'),
                             array('id' => '5', 'pid' => '1'),
                             array('id' => '6', 'pid' => '1'),
                             array('id' => '7', 'pid' => '2'),
                             array('id' => '8', 'pid' => '4', 'pid' => '1')
                             );
 
I am open to that if it makes it simpler, but I still am stumped on as to how to get it into a series of nested <ul> and <li>s.

Any ideas?

Thanks again
Frank

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 7:53 am
by Mark Baker

Code: Select all

 
$items = array(
        array('id' => '1', 'pid' => '0'),
        array('id' => '2', 'pid' => '0'),
        array('id' => '3', 'pid' => '0'),
        array('id' => '4', 'pid' => '1'),
        array('id' => '5', 'pid' => '1'),
        array('id' => '6', 'pid' => '1'),
        array('id' => '7', 'pid' => '2'),
        array('id' => '8', 'pid' => '4')
);
 
function showLevel($items,$parent) {
    $ulSet = False;
    foreach ($items as $item) {
        if ($item['pid'] == $parent) {
            if (!$ulSet) {
                $ulSet = True;
                echo '<ul>';
            }
            echo '<li>'.$item['id'].'</li>';
            showLevel($items,$item['id']);
        }
    }
    if ($ulSet) { echo '</ul>'; }
}
 
showLevel($items,'0');
 

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 8:00 am
by frank_mark
WOW! Thank you immensely Mark.

That worked wonderfully. Now I have to learn it and understand the logic.

If you are ever in the Melbourne, Australia give me a yelp and we'll
go for a beer... my treat.

Thanks again so much.

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 8:21 am
by Mark Baker
frank_mark wrote:WOW! Thank you immensely Mark.

That worked wonderfully. Now I have to learn it and understand the logic.
It's just a simple recursion, nothing clever about it at all. It will work to as many levels as you want, but doesn't test for bad (cyclic) relationships or orphans.
It could also be speeded up by unsetting each entry from the list as it's processed.
frank_mark wrote:If you are ever in the Melbourne, Australia give me a yelp and we'll
go for a beer... my treat.
I'd settle for a glass of water if England win the Ashes this year 8)

Re: Dynamically create a (3 level menu) from array PHP

Posted: Fri Jan 16, 2009 8:36 am
by frank_mark
Well, as helpful as you've been I hope you get that glass of water! Go England!!!