Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Dimensional Arrays and Site Navigation

Status
Not open for further replies.

NigeW

Programmer
Jun 10, 2002
134
NZ
Hi there

Maybe I am re-inventing the wheel but . . .

I am wanting to write a function to reference a multi-dimensional array.

The problem is that I do not know how deep the array might be, so the function needs to be able reference itself so that it finds all entries in the array and at their correct level.

Does anyone have any pointers / ideas ?

Thanks


Nigel Wilson
Christchurch Web Design
 
You can tell how deep an array is using the length property:
Code:
var _arr1 = ["a","b","c","d","e"];
var _arr1_length = _arr1.length;
You have to remain aware that the first index for an array is 0 (not 1) so if you wanted to access the last element of an array, you would access it like this:
Code:
alert(_arr1[_arr1.length - 1]);
Hope this gets you started.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hi Jeff

Thanks for the feedback.

More info is required I think :

I have queried a database and manipulated the data in to the following javascript array :

Code:
var nav = new Array();
nav = [
	['Home','Home'],
	['About Us','About_Us'],
	['Services','Services',
		['Direct Printing','Direct_Printing'],
		['Heat Pressed Transfers','Heat_Pressed_Transfers'],
		['Graphic Design','Graphic_Design'],
		['Embroidery','Embroidery'],
	],
	['Products','Products',
		['Bumper Stickers','Bumper_Stickers'],
		['Tuff Stickers','Tuff_Stickers'],
		['Garment & Product Supply','Garment___Product_Supply'],
		['Reflective & Hi Visibility Options','Reflective___Hi_Visibility_Options'],
		['Reactive Transfer System','Reactive_Transfer_System'],
		['Interactive Race Bibs','Interactive_Race_Bibs',
			['Test Third Level','Test_Third_Level'],
		],
		['Fridge Magnets','Fridge_Magnets'],
	],
	['Bright Ideas','Bright_Ideas'],
	['Clients','Clients'],
	['Newsletter','Newsletter'],
	['Contact Us','Contact_Us']
];


So from here I am wanting to write a javacvsript function to recurse through the arrays and sub arrays (to n levels) and build html output.


=> oh yeah nice looking javascripts on your personal site - very impressive :)

Cheers

Nigel Wilson
Christchurch Web Design
 
You would use a "for" loop at each level of the menu:

Code:
for (var loop1=0; loop1<nav.length; loop1++) {
	var menuLevel1El = nav[loop1];
	// this is where you aceess the 1st level
	for (var loop2=0; loop2<menuLevel1El.length; loop2++) {
		var menuLevel2El = menuLevel1El[loop2];
		// this is where you aceess the 2nd level
		for (var loop3=0; loop3<menuLevel2El.length; loop3++) {
			var menuLevel3El = menuLevel2El[loop2];
			// this is where you aceess the 3rd level
		}
	}
}

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Or, for "n" levels deep, you could use recursion - build a function which takes a parent menu node, and loops through each child, then calls itself for any of those child nodes that has another child node.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
HI there

Thanks to all for their feedback - I have now solved the problem - kept the processing within PHP , rather than extract simple array to Javascript, and then do client side processing - working a treat.

I now have an .htaccess problem - question posted in PHP forum.

Upward and onward !

Nigel Wilson
Christchurch Web Design
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top