Page 1 of 1

multidimensional array + nested foreach = a real mess!!

Posted: Thu Mar 10, 2005 4:19 am
by batfastad
Hi

I'm developing a new navigation script on my site and a preview of it can be seen in action here...
http://www.dealer-world.com/test2.php

The navigation is difficult to explain so you'd better take a look around the page to give it a go first.
It all revolves around detecting which page your currently viewing by checking the values of $p and $s from the address bar.
P is a page, and s is like a subsection of that page.

Some of the links have subsections (AMD and Big Book), but most of them don't.
You'll notice the links with subsections (AMD and Big Book) have multidimensional arrays to list the subsections.
The array data is all delimited by three colons ::: as in each array item there are three bits of data stored.
The type of item - is it a header, an item or a sub(item):::The Text That Goes Into The Menu Bar:::and the corresponding value of s or p that we need to compare the address bar to.

Hopefully you'll get a better idea of what I mean if you check out the links to see how it works.
The comments might be pretty useful within the PHP code.

The script is a real mess.
I've been trying to work this out since before christmas and eventually I've got the behaviour I want from the script but it really is all over the place.

The PHP code itself is at
http://www.dealer-world.com/test2.html

As you can see it's horrific to read through.

I'd appreciate it if someone could read through it to spot any ways it can be shortened/improved.
Maybe there's already PHP functions to do most of what I've had to do manually, but I just don't know enough about PHP to shorten this script.

Any ideas on how this can be improved?

Thanks

Batfastad

Posted: Thu Mar 10, 2005 7:09 am
by Chris Corbyn
I'd suggest posting your PHP code between

Code: Select all

tags in here.

It'll be much easier to read and less hassle

Posted: Thu Mar 10, 2005 7:50 am
by batfastad
Fair enough.
I thought because there's loads of code it might not be acceptable but there's only 120 or so lines.
I'm glad the PHP tags are working again.

Here we go...

Code: Select all

<?php
$p = $_GET["p"];
$s = $_GET["s"];

$menu[0][0] = "item:::AMD:::amd";
$menu[0][1] = "sub:::Advertising Rates:::adrates";
$menu[0][2] = "sub:::Schedule:::schedule";
$menu[0][3] = "sub:::Circulation:::circulation";
$menu[0][4] = "sub:::Editorial Policy:::edpolicy";
$menu[1] = "item:::AMD Directory:::amddirectory";
$menu[2] = "item:::AMD Pro-Guides:::amdproguides";
$menu[3] = "item:::AMD Builder Book:::amdbuilderbook";
$menu[4][0] = "item:::Big Book:::bigbook";
$menu[4][1] = "sub:::About The Big Book:::about";
$menu[4][2] = "sub:::Order:::order";
$menu[4][3] = "sub:::2006/2007:::0607";
$menu[5] = "item:::Dealer World:::dealerworld";
$menu[6] = "item:::IDN Europe:::idneurope";
$menu[7] = "item:::IDN USA:::idnusa";
$menu[8] = "item:::IDN ProBooks:::idnprobooks";
$menu[9] = "item:::Motorcycle Fashion:::motorcyclefashion";

$menu[10] = "header:::Zones";
$menu[11] = "item:::INFOZone:::infozone";
$menu[12] = "item:::PRESSZone:::presszone";
$menu[13] = "item:::SHOWZone:::showzone";

$menu[14] = "header:::'e-Club'";
$menu[15] = "item:::About The 'e-Club':::eclubabout";
$menu[16] = "item:::'e-Club' Archive:::eclubarchive";
$menu[17] = "item:::Subscribe:::eclubsubscribe";

$menu[18] = "header:::Company Info";
$menu[19] = "item:::Advert Design:::advertdesign";
$menu[20] = "item:::Flyer Print & Insert:::flyerdesign";
$menu[21] = "item:::Website Design:::websitedesign";
$menu[22] = "item:::Contact Us:::contactus";
$menu[23] = "item:::Meet The Team:::meettheteam";

foreach ($menu as $i1 => $menuvalue1) {

	if (!is_array($menu[$i1])) {
		// IF CURRENT ARRAY LINE ISN'T AN ARRAY ITSELF
		$menuvalue1exp = explode(":::", $menuvalue1);

		if ($menuvalue1exp[0] == "header") {
			// IF THE MENU ITEM IS A HEADER
			echo("<br />\n<b>".$menuvalue1exp[1]."</b><br />\n");

		} elseif ($menuvalue1exp[2] !== $p) {
			// ITEM NOT SELECTED
			echo("<a href=\"test2.php?p=".$menuvalue1exp[2]."\">".$menuvalue1exp[1]."</a><br />\n");

		} elseif ($menuvalue1exp[2] == $p) {
			// ITEM IS SELECTED
			echo($menuvalue1exp[1]."<br />\n");

		}

	} else {
		// CURRENT ARRAY LINE IS AN ARRAY! MEANS IT HAS SUB ITEMS
		// QUICKLY EXPLODE THE FIRST ONE OF THIS 2ND DIMENSION TO CHECK AGAINST $p
		$menuvalue2exp = explode(":::", $menuvalue1[0]);

		// GET WHICHEVER VALUE OF P WE'RE CURRENTLY ON FROM THE ARRAY
		// SEE LINES 9 OR 17 TO UNDERSTAND THE LINES WE NEED TO DO THIS FOR
		$parentPvalue = $menuvalue2exp[2];

		// IF CURRENT ARRAY ITEM IS SELECTED
		if ($menuvalue2exp[2] == $p) {

			// NOW I GET CONFUSED MYSELF
			foreach ($menu[$i1] as $i2 => $menuvalue2) {
				//EXPLODE THE 2nd DIMENSION
				$menuvalue2exp = explode(":::", $menuvalue2);

				if ($menuvalue2exp[0] == "item") {
					// THIS ONE'S AN ITEM

					if ($menuvalue2exp[2] == $p and $s == "") {
						// ITEM IS SELECTED
						echo($menuvalue2exp[1]."<br />\n");
					} else {
						// ITEM ISN'T SELECTED - MAKE IT A LINK
						echo("<a href=\"test2.php?p=".$menuvalue2exp[2]."\">".$menuvalue2exp[1]."</a><br />\n");
					}

				} else {

					// THIS ONE'S A SUB ITEM
					// NOW WE CHECK TO SEE IF THE SUB ITEM'S SELECTED
					if ($menuvalue2exp[2] == $s) {
						// SUB ITEM IS SELECTED
						echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<small>".$menuvalue2exp[1]."</small><br />\n");
					} else {
						// SUB ITEM ISN'T SELECTED - MAKE IT A LINK
						echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"test2.php?p=".$parentPvalue."&s=".$menuvalue2exp[2]."\"><small>".$menuvalue2exp[1]."</small></a><br />\n");
					}

				}

			}

		} else {
			// ITEM NOT SELECTED
			echo("<a href=\"test2.php?p=".$menuvalue2exp[2]."\">".$menuvalue2exp[1]."</a><br />\n");

		}

	}

}
?>
Any suggestions on how to shorten the loops at the end?

Thanks

Batfastad