Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Dynamically create a (3 level menu) from array
Old 01-16-2009, 09:45 AM Dynamically create a (3 level menu) from array
Junior Talker

Posts: 2
Name: Frank
Trades: 0
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:
  1. $items = array(
  2. array('id' => '1', 'pid' => '0'),
  3. array('id' => '2', 'pid' => '0'),
  4. array('id' => '3', 'pid' => '0'),
  5. array('id' => '4', 'pid' => '1'),
  6. array('id' => '5', 'pid' => '1'),
  7. array('id' => '6', 'pid' => '1'),
  8. array('id' => '7', 'pid' => '2'),
  9. array('id' => '8', 'pid' => '4')
  10. );

Where 'id' index indicates its unique id and 'pid' (parent id) index indicates the parent of the element. So, in the code above, 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
frank_mark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-16-2009, 10:04 AM Re: Dynamically create a (3 level menu) from array
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Why not create a nested array mirroring the hierarchy of the menu?

Something like...

PHP Code:

$item 
= array( 
     array(
'id' => '1''subItems' => array( 'id' => '3''id' => '4') )

something along those lines anyway. Then you can just loop down the levels...
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 01-16-2009, 10:39 AM Re: Dynamically create a (3 level menu) from array
Junior Talker

Posts: 2
Name: Frank
Trades: 0
Thank you Disco Stu for squeezing in such a helpful reply, I know how hectic the Disco life can be.

I hadn't thought of that. I'll give it a go and get back to you.
frank_mark is offline
Reply With Quote
View Public Profile
 
Old 01-19-2009, 08:39 PM Re: Dynamically create a (3 level menu) from array
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
To expand on Stuarts post:

PHP Code:
<?php
$items 
= array();
/*
  The format would be set as:
  $item[parent_id] = array(id)
*/
$items[0] = array(123);
$items[1] = array(456);
$items[2] = array(7);
$items[3] = array();
$items[4] = array(8);

print_r($items);
/*
  Output would be:
  
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )
    [2] => Array
        (
            [0] => 7
        )
    [3] => Array
        (
        )
    [4] => Array
        (
            [0] => 8
        )
)
*/
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Dynamically create a (3 level menu) from array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.81950 seconds with 12 queries