Jump to content

[SOLVED] Multidimensional Array - Breadcrumb Menu


tarun

Recommended Posts

<?php
$menu["Home"] = "/index.php";
     $submenu["Home"]["About"] = "/about.php";

$menu["Contact Us"] = "/contactus.php";
     $submenu["Contact Us"]["Live Chat"] = "/chat/index.php";
          $submenu["Contact Us"]["Live Chat"]["Live Help"] = "/chat/connected.php";
     $submenu["Contact Us"]["Email"] = "/emailform.php";

$menu["Members"] = "/members/index.php";
     $submenu["Members"]["Profile"] = "/members/profiles/index.php";
          $submenu["Members"]["Profile"]["View Profile"] = "/members/profiles/viewprofile.php";
          $submenu["Members"]["Profile"]["View All Profiles"] = "/members/profiles/viewall.php";
          $submenu["Members"]["Profile"]["Edit Profile"] = "/members/profiles/edit.php";
     $submenu["Members"]["Check Messages"] = "/members/mail.php";
     $submenu["Members"]["Send A Message"] = "/members/send.php";
     $submenu["Members"]["Edit Profile"] = "/members/editprofile.php";

?>

Well... Thats My Multi Dimensional Array

How Would I Go About Making A Breadcrumb Menu From It

 

In Case You Didn't No A Breadcrumb Menus Are Like This:

[iurl=#]Members[/iurl] / [iurl=#]Profile[/iurl] / [iurl=#]Edit Profile[/iurl]

Link to comment
Share on other sites

The best way of doing this with an array would be to keep track of exactly where the user is in a session. That way you don't have to search the array for what page the user is on and it will work if the page is directed to from multiple locations.

Link to comment
Share on other sites

Well, with the array set up as you have it, you'll run into problems. You can't have an element of an array as BOTH a string and an array. For instance, you have

$submenu["Contact Us"]["Live Chat"] equal to the string "/chat/index.php" and it is also an array.

 

If you were to modify your $submenu array so that it only ever has two dimensions, the first being the parent node, the second the child, you can write a recursive function similar to:

 

<?php

function menu($menu,$index='Parent'){
foreach($menu[$index] as $k => $v){
	echo "<li><a href='$v'>$k</a></li>";
		if(is_array($menu[$k])){
		echo '<ul>';
		menu($menu,$k);
		echo '</ul>';
	}
}	
}
$menu = array();
$menu["Parent"]["Home"] = "/index.php";
$menu["Parent"]["Contact Us"] = "/contactus.php";
$menu["Parent"]["Members"] = "/members/index.php";
$menu["Home"]["About"]  = "/about.php";
$menu["Contact Us"]["Live Chat"] = "/chat/index.php";
$menu["Live Chat"]["Live Help"] = "/chat/connected.php";
$menu["Contact Us"]["Email"] = "/emailform.php";

echo '<ul>';
menu($menu);
echo '</ul>';
?>

 

Give that a shot.

 

P.S. 1000th post :P

 

EDIT: Reposted with a neater function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.