Page 1 of 1

Multidimensional array - heirarchical menu system...

Posted: Wed Dec 15, 2004 1:44 pm
by batfastad
Hi

I've been told that multidimensional arrays offer the best way to do what I'm trying to get here.

I'm building a new site using PHP and I want to have a heirarchical menu structure.
The problem is I'm having trouble getting to the second and third dimension data. I can't work out how to do it.

Here's the basic structure of my menu...

= categories
== links
====sections

Categories *never* have urls in the menu. Only links and sections are clickable menu items.
Sections are only displayed when the current page is either the parent 'link', or another section in the same link.

I'm having trouble just thinking about how to organise my multidimensional array in the first place.

So you could have a menu like this...

Code: Select all

=category1
==link1
====section1
====section2
====section3
==link2
==link3
==link4
====section1
====section2
==link5
====section1
====section2
=category2
==link1
==link2
====section1
==link3
=category3
==link1
==link2
==link3
and so on.

This is how I've got my array structured so far...

Code: Select all

$menu['cat1'] = '';
$menu['cat1']['link1'] = 'http://url';
$menu['cat1']['link2'] = 'http://url';
$menu['cat2'] = '';
$menu['cat2']['link1'] = 'http://url';
$menu['cat2']['link1']['section1'] = 'http://url';
$menu['cat2']['link1']['section2'] = 'http://url';
$menu['cat2']['link1']['section3'] = 'http://url';
$menu['cat2']['link2'] = 'http://url';
$menu['cat2']['link2']['section1'] = 'http://url';
$menu['cat2']['link3'] = 'http://url';
$menu['cat2']['link4'] = 'http://url';
$menu['cat2']['link5'] = 'http://url';
$menu['cat2']['link5']['section1'] = 'http://url';
$menu['cat2']['link5']['section2'] = 'http://url';
$menu['cat2']['link5']['section3'] = 'http://url';
$menu['cat2']['link5']['section4'] = 'http://url';
$menu['cat2']['link5']['section5'] = 'http://url';
$menu['cat3'] = '';
$menu['cat3']['link1'] = 'http://url';
$menu['cat3']['link2'] = 'http://url';
$menu['cat3']['link2']['section1'] = 'http://url';
$menu['cat3']['link2']['section2'] = 'http://url';
$menu['cat3']['link3'] = 'http://url';
But I'm not sure if I've got this set up the correct way for what I want.

I know I'll then have to set up a nested loop. Then in the nested loop I will then test to see if the url in the array equals the current page and either display the sections, or not. And highlight the section/link as the current page (in bold or something).

Any advice as to how I need to arrange the array?

I'm so confused now. I've tried setting it up so many different ways then found there are different reasons for setting it up in different ways - and it's become a bit of a nightmare.
I'd like to get it started correctly so I don't have to backtrack later on.

Thanks in advance.

Ben

Posted: Wed Dec 15, 2004 3:51 pm
by rehfeld
well, first of all

$menu['cat1'] = '';

is pointless. your declaring it an empty string, but then if you do
$menu['cat1'][link1']
you have just redeclared it as an array


second, if your going to have some links have more than 1 section, i would structure the array so that all links have a "section"

otherwise its going to be cumbersome to loop through things,

maybe something like this:

Code: Select all

$menu = array(
    'category_name1' => array(
                        'section_name1' = array(
                               'http://url1',
                               'http://url2',
                               'http://url3',
                          ),
                        'section_name2' = array(
                               'http://url',
                          ),
                     ),
    'category_name2' => array(
                        'section_name1' = array(
                               'http://url',
                          ),
                        'section_name2' = array(
                               'http://url',
                          ),
                     ),
);
even if a category only has 1 section, use an array. that way you can loop through it.

same goes for if a section only has 1 link, use an array so you can still loop