sub-array... site navigation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

sub-array... site navigation

Post by batfastad »

Hi

This is a fairly quick question.

I'm designing a PHP menu system based on an array of items.

The problem is I want to have submenus and so on.
The way I've decided to do this is in the array...

Code: Select all

<?php

$menu['Home'] = 'cat|';
$menu['News'] = 'item|news.php';
$menu['Archive'] = 'item|archive.php';
$menu['Publications'] = 'cat|';
$menu['AMD'] = 'item|testamd.php';
$menu['Editorial Policy'] = 'sub|test1.php';
$menu['Advertising'] = 'sub|test2.php';
$menu['Schedule'] = 'sub|test3.php';
Now the array value you notice the pipe character. I'm using that as a delimeter so I can store lots of information for each item in the array.

The first value before the | character is what type of menu item this is.

It can either be cat, item or sub. Cat is a category heading, item is a normal menu item, sub is a submenu item. And they'll work in that heirarchical order.

The next value after the | pipe character is the URL.

Now I may end up with 70 or so items in this array.
Obviously only a selection of those will be visible as items marked 'sub' will only be visible if the parent page is being displayed.
I have that sorted though I think.

The next section of my code is to go through each item in the array, do explode() on each array value to parse each item before and after the | delimeter into a sort of sub-array (I know that's not a very technical term but it kindof explains what I'm driving at I think).
Then if the current page equals the URL after one of the | it will show the submenus, if not then it won't.

My question is this...
$menu might end up with 70-100 items in this array.
Will there be a massive processing overhead in parsing each array value into a sub-array based on the | separator I've chosen?

Don't forget the values of each one of these $menu items will have to be split into two other values either side of the | delimeter.

Is this a feasible way to approach this or is there an easier way?

I don't want to add massive processing time to my page.

I thought that setting up the menu in this way is one of the easiest ways for me to update but if anyone has any other suggestions then please let me know.

I don't want to go down the line of a database driven menu structure as I don't think the site will become large enough.

Is there a faster way to do this than looping and parsing each array value into another sub-array?
I'm worried that for 70-100 array items there could be noticeable slowdown.

Any suggestions?

Thanks

Ben
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i would use a multi-dimensional array...

just as you add multiple menu items for the first column the array...

now add sub menu items to the 2nd column

menu => submenu1 => subsubmenu1.1

$menu => $menu[0] => $menu[0][0]
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Hi mate,

Multidimensional arrays sound great!

Just had a look around and got a bit confused though.

I'm not sure how I should re-write the array in my case.

Can I do it like this?

Code: Select all

$menu['Home']['type'] = 'cat';
$menu['Home']['url'] = '';
$menu['News']['type'] = 'item';
$menu['News']['url'] = 'http://www.domain.com/home';
$menu['AMD Magazine']['type'] = 'item';
$menu['AMD Magazine']['url'] = 'http://www.domain.com/amd';
$menu['Editorial Policy']['type'] = 'sub';
$menu['Editorial Policy']['url'] = 'http://www.domain.com/editorialpolicy';
$menu['Advertising']['type'] = 'sub';
$menu['Advertising']['url'] = 'http://www.domain.com/advertising';
$menu['Schedule']['type'] = 'sub';
$menu['Schedule']['url'] = 'http://www.domain.com/schedule';
In my original post I had the array formatted as...
$menu['text'] = 'type|url';

Is that the best way to re-arrange this?

How then do I loop through the multidimensional array?

Do I need to do a nested loop of some kind?

Thanks for your help

Ben
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

yes a nested loop would be what you need.

try using print_r() on the main array so you can see the structure, and devise your plan from there.
Post Reply