1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Array Menu

Discussion in 'PHP' started by Omzy, Nov 30, 2008.

  1. #1
    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...
     
    Omzy, Nov 30, 2008 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,981
    Likes Received:
    4,572
    Best Answers:
    124
    Trophy Points:
    665
    #2
    catching up on your homework?
     
    sarahk, Nov 30, 2008 IP
  3. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #3
    can you give us output example (on html tag)...?
     
    misbah, Nov 30, 2008 IP
  4. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Basically it needs to output UL tags for each $menu and LI tags for each $items
     
    Omzy, Dec 1, 2008 IP
  5. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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:
     
    elias_sorensen, Dec 1, 2008 IP
  6. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can it be done using the key() function at all?
     
    Omzy, Dec 1, 2008 IP
  7. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Use the code I made, that works perfectly (got it tested) :)

    Key wont be your solution.
     
    elias_sorensen, Dec 1, 2008 IP
  8. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for that bro :)
     
    Omzy, Dec 1, 2008 IP
  9. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    You're welcome ;)
     
    elias_sorensen, Dec 1, 2008 IP
  10. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Can the $menu and $items arrays be combined at all?
     
    Omzy, Dec 1, 2008 IP
  11. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Any idea how the $menu and $items arrays be combined to acheive the same result?
     
    Omzy, Dec 2, 2008 IP
  12. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #12
    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.
     
    elias_sorensen, Dec 2, 2008 IP
  13. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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?
     
    Omzy, Dec 2, 2008 IP
  14. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #14
    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. :)
     
    elias_sorensen, Dec 2, 2008 IP
  15. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    Omzy, Dec 2, 2008 IP
  16. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #16
    Ahh.. Ha ha... That wasn't even the question... My bad :D

    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:
     
    elias_sorensen, Dec 2, 2008 IP
  17. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Thank you ever so much again. You really are very skilled in PHP! :)
     
    Omzy, Dec 2, 2008 IP
  18. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #18
    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 ;)
     
    elias_sorensen, Dec 2, 2008 IP