Basically I need to create a 2 tier menu with the menu data in an array. I will then use a foreach loop to match the main menu with it's items. Something along the lines of this: $menu = array('1'=>'MENU1', '2'=>'MENU2', '3'=>''MENU3'); $items = array( '1'=> array('1'=>'Menu 1 Item 1', '2'=>'Menu 1 Item 2', '3'=>'Menu 1 Item 3'), '2'=> array('1'=>'Menu 2 Item 1', '2'=>'Menu 2 Item 2', '3'=>'Menu 2 Item 3') ); Code (markup): What the code needs to do is cycle through each loop and assign the items to it's corresponding main menu. I have to do this in an array but I'm not sure if I'm doing it correct here...
Just made this quick in my math lesson (lol), it should work, but not tested <? $menu = array('1'=>'MENU1', '2'=>'MENU2', '3'=>'MENU3'); $items = array( '1'=> array('1'=>'Menu 1 Item 1', '2'=>'Menu 1 Item 2', '3'=>'Menu 1 Item 3'), '2'=> array('1'=>'Menu 2 Item 1', '2'=>'Menu 2 Item 2', '3'=>'Menu 2 Item 3') ); for($i = 1;$i <= count($menu);$i++){ //Count how many menus you have echo "<b>Menu ".$i."</b><br />"; echo "<ul>"; for($x = 1;$x <= count($items[$i]);$x++){ //Count how menu items there are to the corresponding menu. echo "<li>".$items[$i][$x]."</li>"; } echo "</ul>"; } ?> PHP:
You'll just have to loop through each array and their sub-arrays. But I can't see the idea by doing that. It will quickly become very unstructured. I would keep them in two seperate arrays.
Cheers for that mate. On a slightly different problem now - I have used that same array and outputted it on another page as a INPUT TYPE=CHECKBOX list. The thing is it displays all the data in one long list - how can I get it to split up the data and display it in 2/3 columns on the page?
Just put a <br /> after the line with the checkboxes, before: echo "<li>".$items[$i][$x]."</li>"; PHP: after: echo '<input type="checkbox" name="checkbox[]" id="'.$x.'"><label for="'.$x.'">'.$items[$i][$x].'</label><br />'; PHP: I have putted the text inside a label element, so if you click on the text next to a checkbox, it will automaticly check/uncheck that checkbox.
Yeah I've done exactly that but it just displays everything in one long list, I basically want to tell it to display the data across 2/3 columns on the page.
Ahh.. Ha ha... That wasn't even the question... My bad I would do it like this: <? $menu = array('1'=>'MENU1', '2'=>'MENU2', '3'=>'MENU3'); $items = array( '1'=> array('1'=>'Menu 1 Item 1', '2'=>'Menu 1 Item 2', '3'=>'Menu 1 Item 3'), '2'=> array('1'=>'Menu 2 Item 1', '2'=>'Menu 2 Item 2', '3'=>'Menu 2 Item 3') ); echo "<div style=\"float:left;width:33%;\">"; //We'll start the first column. Whole page = 100%, every colums is 33% = 3 colums for($i = 1;$i <= count($menu);$i++){ //Count how many menus you have if($i == 5 || $i == 10){ //We'll start a new column for every 5th menu. echo "</div><div style=\"float:left;width:33%;\">"; //End the current column and start a new one. } echo "<b>Menu ".$i."</b><br />"; echo "<ul>"; for($x = 1;$x <= count($items[$i]);$x++){ //Count how menu items there are to the corresponding menu. echo '<input type="checkbox" name="checkbox[]" id="'.$x.'"><label for="'.$x.'">'.$items[$i][$x].'</label><br />'; } echo "</ul>"; } echo "</div><br style=\"clear:both\" />"; //End the final column and make sure to clear:both, if not, all other content will float to the last div because if our "nasty" float:left's. :) ?> PHP:
Thanks for the compliment Worked with PHP for 5 years now (since I were 11 ), so I am glad to hear that someone appreciate it