searching within arrayv - multidimensional needed?...

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

searching within arrayv - multidimensional needed?...

Post by batfastad »

Hi

I wonder if you guys can help me with this.
I'm setting up a heirarchical menu system on my site.

I have an array...

Code: Select all

$menu['News'] = 'item||http://www.dealer-world.com/news.php';
$menu['Archive'] = 'item||archive.php';
$menu['Publications'] = 'cat||';
$menu['AMD'] = 'item||testamd.php';
$menu['Editorial Policy'] = 'sub||test.php';
$menu['Advertising'] = 'sub||test2.php';
$menu['Schedule'] = 'sub||test3.php';
$menu['AMD Directory'] = 'item||amddirectory.php';
$menu['AMD Pro-Guides'] = 'item||amdproguides.php';
$menu['AMD Builder Book'] = 'item||amdbuilderbook.php';

// PARSE EACH ARRAY ITEM INTO $menutext AND $menuvalue
foreach ($menu as $menutext => $menuvalue) { 

	// EXPLODE EACH $menuvalue INTO ANOTHER ARRAY DIMENSION BASED ON THE || DOUBLE PIPE DELIMETER
	$menuvaluearray = explode("||", $menuvalue);
	$menutype = $menuvaluearray['0'];
	$menuurl = $menuvaluearray['1'];
So basically I've got my array set up so that there are two values stored in each array value, by using a delimeter || between each.

The first value (before the delimeter) is what type of menu item it is.
There are 3 types... cat/item/sub and they have the following heirarchy...

Code: Select all

cat
--item
--item
--item
cat
--item
--item
----sub
----sub
----sub
--item
--item
----sub
----sub
Now as this is a heirarchical menu I need it so that subs are only shown if anything under their parent 'item' is clicked.

So when I'm looping through I'm doing a check on each row in the array.

Firstly to see what menutype the item is (cat/item/sub)
Then if it's item/sub I'm doing this...

Code: Select all

if (strstr($_SERVER["SCRIPT_URI"], $menuurl)) {
// CURRENT URL MATCHES $menuurl
	} else {
// NO MATCH
	};
This is not quite what I need to do. I realise now that I need to see what the next values in the array are whilst I'm looping through it.

Obviously the child rows with $menuitem = 'sub' are only shown when either...
a) the parent item is selected
b) another child 'sub' is selected
And only one item's subs can be shown at a time.

So whether the sub is shown depends where you are in the heirarchy whilst looping through - and the only way to find that out is to be able to access values in the array whilst you are looping through.

I think this method I've got is getting far too complicated and there must be a better way. I thought of setting up a multi-dimensional array but I didn't know how to work it with what I'm trying to achieve.
I'm hoping that my explanation is clear enough for someone to understand what it is I'm trying to get going here.

Any ideas?

Thanks in advance guys and gals.

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

Post by timvw »

imho, multidimensional arrays are the way to go in this case

offcourse some websearching and reading will help you too :)

http://www.desilva.biz/arrays/multidimen.html
http://www.hypermedic.com/php/multarray.php
...
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

I still can't think of how to apply it to this situation.

I need to read ahead in the array so that when I come to a particular step in the item loop, I can tell it to display the next sub items,or skip over those array rows completely.

It doesn't seem to matter whether it's in a multidimensional or not - I'm still confused how I actually get at the data in the way that I need.

I'll have 3 nested loops...

Code: Select all

Loop1 {

     Loop2 {
// CODE HERE

          Loop3 {


          };

     };

};
The code in loop 3 will be evaluating what happens in the third dimension of the array - the sub items underneath an item.

But I need to know at the beginning of loop 2 whether any of the child sub items are the current page or not.

The code needs to be at the beginning of loop 2 as well, as otherwise the item header will appear underneath its sub-items.

I mean it's no problem for me to detect if the item has any sub items, as I can just see if that dimension of the array is an array itself

Code: Select all

// BEGIN LOOP 2
if ($arraylevel2 == 'Array') {
   echo("This item has sub items but how do I know if one of them is the current page?? I can't use get variables to do this either otherwise i would have finished this last week");

          // BEGIN LOOP 3
          // DO STUFF FOR SUB ITEMS
          foreach (...) {

          };

} else {
   echo("This item has no sub items - make it a normal link and do some other stuff");
   // DON'T NEED TO BOTHER DOING A THIRD LOOP - IT'S NOT AN ARRAY SO THERE IS NO DATA
};
That's where I'm having the problem.
I can't see how the array being multidimensional has solved that problem at all.

Any ideas?

Thanks

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

Post by timvw »

If you insist on doing it the painfull way:

Code: Select all

$menu = array();
$menu[] = array('label' => 'News', 'type' => 'item' , 'url' => 'http://www.dealer-world.com/news.php');
$menu[] = array('label' => 'Archive', 'type' => 'item', 'url' =>'archive.php');
...
And now loop through the array

Code: Select all

for ($i = 0; $i < sizeof($menu); ++$i)
{
       // we are at element $menu[$i]
       // we can have a look at menu[$i+1] to see if it is a sub menu of the current
}
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

How would I do this a less painful way?

I've got this set up as a multidimensional array now...

Code: Select all

$menu['Publications'] = '';
$menu['Publications']['AMD'] = 'amd.php';
$menu['Publications']['AMD']['Editorial Policy'] = 'test.php';
$menu['Publications']['AMD']['Advertising'] = 'test2.php';
$menu['Publications']['AMD']['Schedule'] = 'test3.php';
$menu['Publications']['AMD Directory'] = 'amddirectory.php';
$menu['Publications']['AMD Pro-Guides'] = 'amdproguides.php';
$menu['Publications']['AMD Builder Book'] = 'amdbuilderbook.php';
$menu['Publications']['IDN Europe'] = 'idneurope.php';
$menu['Publications']['IDN Europe']['Editorial Policy'] = 'test.php';
$menu['Publications']['IDN Europe']['Advertising'] = 'test2.php';
$menu['Publications']['IDN Europe']['Schedule'] = 'test3.php';
$menu['Publications']['IDN USA'] = 'idnusa.php';
$menu['Publications']['IDN Big Book'] = 'idnbigbook.php';
$menu['Publications']['IDN ProBooks'] = 'idnbigbook.php';
$menu['Publications']['Motorcycle Fashion'] = 'idnbigbook.php';
This shows a category called 'Publications', and 9 items listed under publications.

I see that from the way you've redone the array above you've done it so the key of the array is a number so you can then access $i+1, $i+2, $i+3 $i+4 (If I say there's a maximum of 4 sub items).

How would I loop through this multidimensional array?
How do I restructure the array into a multidimensional with the key as a number?
Rather than a string (Publication) like how I've got it set up above.

Also the way you've got the array set up above...

Code: Select all

$menu[] = array('label' => 'Archive', 'type' => 'item', 'url' =>'archive.php');
Is that a syntax so that an array can hold more than one bit of data?

I'm still very new to PHP and am finding it really difficult.

Could I do this...

Code: Select all

$menu[] = 'Publications||';
$menu[][] = 'AMD||amd.php';
$menu[][][] = 'Editorial Policy||test.php';
$menu[][][] = 'Advertising||test2.php';
$menu[][][] = 'Schedule||test3.php';
$menu[][] = 'AMD Directory||amddirectory.php';
$menu[][] = 'AMD ProGuides||amdproguides.php';
$menu[][] = 'AMD Builder Book||amdbuilderbook.php';
$menu[][] = 'IDN Europe||idneurope.php';
$menu[][][] = 'Editorial Policy||test.php';
$menu[][][] = 'Advertising||test2.php';
$menu[][][] = 'Schedule||test3.php';
$menu[][] = 'IDN USA||idnusa.php';
$menu[][] = 'IDN Big Book||idnbigbook.php';
$menu[][] = 'IDN ProBooks||idnprobooks.php';
$menu[][] = 'Motorcycle Fashion||fashion.php';
Then loop through in the way you suggested, testing $i+1, $i+2, $i+3, $i+4 each time. But using explode() to get the two values.

I think I'll try and do my idea above and get back to you.

If you could let me know if I'm on the right tracks then I'd appreciate it.

Thanks

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

Post by rehfeld »

another thread similar to this

viewtopic.php?t=28696
Post Reply