Dynamically create a (3 level menu) from array PHP
Posted: Fri Jan 16, 2009 7:02 am
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:
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
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')
);
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