Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Not Looping through full array.

    First is this Array code valid, I think it's probably not. What I am trying to do is loop through the array to build a Navigation Menu. The buildMenu function will loop the main_menu array but not the sub_menu array, I'm just a PHP Learner and am lost with this any help would be great. Thanks

    PHP Code:
    //MENU ARRAY
    $main_menu[0] = array('name' => 'HOME PAGE''link' => 'index.php');
    $main_menu[1] = array('name' => 'MENU 2''link' => 'index.php?id=2');
    $main_menu[2] = array('name' => 'MENU 3''link' => 'index.php?id=3');
    $main_menu[3] = array('name' => 'MENU 4''link' => 'index.php?id=4');
    $main_menu[4] = array('name' => 'MENU 5''link' => 'index.php?id=5');

    $menu_menu[1] = Array(
                
    'sub_menu' => Array('name' => 'SUB MENU 1''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 2''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 3''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 4''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 5''link' => 'index.php'),
            );
            
    $menu_menu[2] = Array(
                
    'sub_menu' => Array('name' => 'SUB MENU 1''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 2''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 3''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 4''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 5''link' => 'index.php'),
            );


    function 
    buildMenu($main_menu)
    {
        foreach (
    $main_menu as $key => $list)
        {
            echo 
    "\n<li><a href='"$list['link'] ."'>"$list['name'] ."</a>";
            if ( ! empty(
    $key['sub_menu'])) {
                echo 
    "<ul>";
                
    buildMenu($key['sub_menu']);
                echo 
    "</ul>";
            }
            echo 
    "</li>";
        }


  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    variable scope... your function can not 'see' $menu_menu

    so either use global
    PHP Code:
    function buildMenu($main_menu){ 
        global 
    $menu_menu
    or pass both arrays to the function

    PHP Code:
    function buildMenu($main_menu,$menu_menu)
    {
        foreach (
    $main_menu as $key => $list)
        {
            echo 
    "\n<li><a href='"$list['link'] ."'>"$list['name'] ."</a>";
            if ( ! empty(
    $key['sub_menu'])) {
                echo 
    "<ul>";
                
    buildMenu($key['sub_menu'],$menu_menu);
                echo 
    "</ul>";
            }
            echo 
    "</li>";
        }

    I have not actually tested the loop but thats the main issue
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    trazix (11-18-2012)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks for the reply firepages, however no joy. I'v made a boo boo in the Array I posted. It should have looked like this,

    PHP Code:
    //MENU ARRAY
    $main_menu[0] = array('name' => 'HOME PAGE''link' => 'index.php');
    $main_menu[1] = array('name' => 'MENU 2''link' => 'index.php?id=2');
    $main_menu[2] = array('name' => 'MENU 3''link' => 'index.php?id=3');
    $main_menu[3] = array('name' => 'MENU 4''link' => 'index.php?id=4');
    $main_menu[4] = array('name' => 'MENU 5''link' => 'index.php?id=5');

    $main_menu[1] = Array(
                
    'sub_menu' => Array('name' => 'SUB MENU 1''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 2''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 3''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 4''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 5''link' => 'index.php'),
            );
            
    $main_menu[2] = Array(
                
    'sub_menu' => Array('name' => 'SUB MENU 1''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 2''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 3''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 4''link' => 'index.php'),
                
    'sub_menu' => Array('name' => 'SUB MENU 5''link' => 'index.php'),
            ); 
    Would this be a valid multidimensional array.
    Last edited by trazix; 11-16-2012 at 02:20 PM.

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Still trying to solve this with not much luck ,my new array looks like this which i think is a valid type of array.

    PHP Code:
    //MAIN MENU ARRAY
    $main_menu[0] = array('name' => 'HOME PAGE''link' => 'index.php');
    $main_menu[1] = array('name' => 'MENU 2''link' => 'index.php?id=2');
    $main_menu[2] = array('name' => 'MENU 3''link' => 'index.php?id=3');
    $main_menu[3] = array('name' => 'MENU 4''link' => 'index.php?id=4');
    $main_menu[4] = array('name' => 'MENU 5''link' => 'index.php?id=5');
    //SUB MENU ARRAY
    $main_menu[1][0] = array('name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][1] = array('name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][2] = array('name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][3] = array('name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][4] = array('name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx');
    //SUB MENU ARRAY
    $main_menu[2][0] = array('name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][1] = array('name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][2] = array('name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][3] = array('name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][4] = array('name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx'); 
    PHP Code:
    foreach ($main_menu as $key => $keyValue) {
        foreach (
    $keyValue as $keyValuex) {
            echo 
    "\n<li><a href='"$keyValuex['link'] ."'>"$keyValuex['name'] ."</a>";
        }

    When I try this foreach loop I get output like.

    Code:
    H
    i
    M
    i
    Sub Menu 1
    Sub Menu 2
    Sub Menu 3
    Sub Menu 4
    Sub Menu 5
    M
    i
    Sub Menu 1
    Sub Menu 2
    Sub Menu 3
    Sub Menu 4
    Sub Menu 5
    M
    i
    M
    i
    Html source code.
    Code:
    <li><a href='H'>H</a>
    <li><a href='i'>i</a>
    <li><a href='M'>M</a>
    <li><a href='i'>i</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a>
    <li><a href='M'>M</a>
    <li><a href='i'>i</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a>
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a>
    <li><a href='M'>M</a>
    <li><a href='i'>i</a>
    <li><a href='M'>M</a>
    What I would like is to have the foreach loop output a unordered list code like this.

    Code:
    <ul id="nav">
    
    <li><a href="index.php">Home</a></li>
    
    	<li><a class="hsubs" href="index.php?id=1">Menu 1</a>
    		<ul class="subs">
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 1</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 4</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 5</a></li>
    		</ul>
    	</li>
    
    	<li><a class="hsubs" href="index.php?id=2">Menu 2</a>
    		<ul class="subs">
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-1</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-2</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-3</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-4</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-5</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-6</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-7</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 2-8</a></li>
    		</ul>
    	</li>
    	
    	<li><a class="hsubs" href="index.php?id=3">Menu 3</a>
    		<ul class="subs">
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3-1</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3-2</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3-3</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3-4</a></li>
                            <li><a href="index.php?id=xxxxxxxxxxx">Submenu 3-5</a></li>
    		</ul>
                    </li>
    
                    <li><a href="index.php?id=4">Menu 4</a></li>
    
                    <li><a href="index.php?id=5">Menu 5</a></li>
    
                    <li><a href="index.php?id=6">Menu 6</a></li>
    
                    <li><a href="index.php">Back</a></li>
    </ul>
    This is beyond my current php abilities.
    Last edited by trazix; 11-18-2012 at 04:07 PM.

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Trying this loop now. I think I am on the right track but just cant get it to work.
    PHP Code:
    echo "<ul id=\"nav_menu\">";

    foreach (
    $main_menu as $key => $keyValue)
    {
            echo 
    "\n<li class=\"YYYY\"><a href='"$keyValue['link'] ."'>"$keyValue['name'] ."</a>";
                 { echo 
    "</li><ul class=\"XXXX\">";
                   foreach (
    $keyValue as $keyValuex)   {
                           echo 
    "\n<ul class=\"ZZZZ\"><li><a href='"$keyValuex['link'] ."'>"$keyValuex['name'] ."</a></li>";

                                                       }
                 }
               echo 
    "</ul>";
    }
    echo 
    "</ul>"
    This is the source code i am getting, the <ul> tags are not in the right places.
    Code:
    <ul id="nav_menu">
    <li class="YYYY"><a href="index.php">HOME PAGE</a></li><ul class="XXXX">
    <ul class="ZZZZ"><li><a href="H">H</a></li>
    <ul class="ZZZZ"><li><a href="i">i</a></li></ul>
    <li class="YYYY"><a href="index.php?id=2">MENU 2</a></li><ul class="XXXX">
    <ul class="ZZZZ"><li><a href="M">M</a></li>
    <ul class="ZZZZ"><li><a href="i">i</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 1</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 2</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 3</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 4</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 5</a></li></ul>
    <li class="YYYY"><a href="index.php?id=3">MENU 3</a></li><ul class="XXXX">
    <ul class="ZZZZ"><li><a href="M">M</a></li>
    <ul class="ZZZZ"><li><a href="i">i</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 1</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 2</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 3</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 4</a></li>
    <ul class="ZZZZ"><li><a href="index.php?id=xxxxxxxxxxx">Sub Menu 5</a></li></ul>
    <li class="YYYY"><a href="index.php?id=4">MENU 4</a></li><ul class="XXXX">
    <ul class="ZZZZ"><li><a href="M">M</a></li>
    <ul class="ZZZZ"><li><a href="i">i</a></li></ul>
    <li class="YYYY"><a href="index.php?id=5">MENU 5</a></li><ul class="XXXX">
    <ul class="ZZZZ"><li><a href="M">M</a></li>
    <ul class="ZZZZ"><li><a href="i">i</a></li></ul></ul>

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    echo "<ul id=\"nav_menu\">"

    foreach (
    $main_menu as $key => $keyValue

      echo 
    "\n<li class=\"YYYY\"><a href='"$keyValue['link'] ."'>"$keyValue['name'] ."</a>";
      echo 
    "\n<ul class=\"XXXX\">";  
      foreach (
    $keyValue as $keyValuex)
      {
         echo 
    "\n<li><a href='"$keyValuex['link'] ."'>"$keyValuex['name'] ."</a></li>"
      } 
      echo 
    "</ul></li>"

    echo 
    "</ul>"

  • Users who have thanked poyzn for this post:

    trazix (11-18-2012)

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks Poyzn.
    I'v been working on my own loop since and I'm getting a similar result to your's.
    PHP Code:
    echo "\n\n\n<ul id=\"nav_menu\"><!-- Unordered List Start Tag -->\n";

    foreach (
    $main_menu as $key => $keyValue)
    {
            echo 
    "\n<li class=\"main_menu\"><a href='"$keyValue['link'] ."'>"$keyValue['name'] ."</a>";
                 { echo 
    "\n\n<ul class=\"sub_menu\">\n";
                   foreach (
    $keyValue as $keyValuex)   {
                           echo 
    "\n<li><a href='"$keyValuex['link'] ."'>"$keyValuex['name'] ."</a></li>\n";

                                                       }
                                                       { echo 
    "</ul><!-- Sub Menu Unordered List Item Closing Tag -->";
                 }
                 } { echo
    "\n</li><!-- Sub Menu List Item Closing Tag -->\n";
              
    //echo "</li>";

    }
    }
    echo 
    "\n</ul><!-- Unordered List End Tag -->\n"
    Code:
    <ul id="nav_menu"><!-- Unordered List Start Tag -->
    
    <li class="main_menu"><a href='index.php'>HOME PAGE</a>
    
    <ul class="sub_menu">
    
    <li><a href='H'>H</a></li>
    
    <li><a href='i'>i</a></li>
    </ul><!-- Sub Menu Unordered List Item Closing Tag -->
    </li><!-- Sub Menu List Item Closing Tag -->
    
    <li class="main_menu"><a href='index.php?id=2'>MENU 2</a>
    
    <ul class="sub_menu">
    
    <li><a href='M'>M</a></li>
    
    <li><a href='i'>i</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a></li>
    </ul><!-- Sub Menu Unordered List Item Closing Tag -->
    </li><!-- Sub Menu List Item Closing Tag -->
    
    <li class="main_menu"><a href='index.php?id=3'>MENU 3</a>
    
    <ul class="sub_menu">
    
    <li><a href='M'>M</a></li>
    
    <li><a href='i'>i</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a></li>
    
    <li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a></li>
    </ul><!-- Sub Menu Unordered List Item Closing Tag -->
    </li><!-- Sub Menu List Item Closing Tag -->
    
    <li class="main_menu"><a href='index.php?id=4'>MENU 4</a>
    
    <ul class="sub_menu">
    
    <li><a href='M'>M</a></li>
    
    <li><a href='i'>i</a></li>
    </ul><!-- Sub Menu Unordered List Item Closing Tag -->
    </li><!-- Sub Menu List Item Closing Tag -->
    
    <li class="main_menu"><a href='index.php?id=5'>MENU 5</a>
    
    <ul class="sub_menu">
    
    <li><a href='M'>M</a></li>
    
    <li><a href='i'>i</a></li>
    </ul><!-- Sub Menu Unordered List Item Closing Tag -->
    </li><!-- Sub Menu List Item Closing Tag -->
    
    </ul><!-- Unordered List End Tag -->

    I have 2 issues, first should the Menu's that dont have a Sub menu array not have closing </li> tag's ,for example the first Home link
    <li class="main_menu"><a href='index.php'>HOME PAGE</a>
    should it be <li class="main_menu"><a href='index.php'>HOME PAGE</a></li>

    The second issue is for some reason the Loop is creating 2 unwanted List items at the start of each UL , example
    Code:
    <li><a href='H'>H</a></li>
    
    <li><a href='i'>i</a></li>

  • #8
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by trazix View Post
    I have 2 issues, first should the Menu's that dont have a Sub menu array not have closing </li> tag's ,for example the first Home link
    <li class="main_menu"><a href='index.php'>HOME PAGE</a>
    should it be <li class="main_menu"><a href='index.php'>HOME PAGE</a></li>
    Yes, it should has closing tag

    Quote Originally Posted by trazix View Post
    The second issue is for some reason the Loop is creating 2 unwanted List items at the start of each UL , example
    Code:
    <li><a href='H'>H</a></li>
    
    <li><a href='i'>i</a></li>
    Show your $main_menu variable.

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    The the following code:

    PHP Code:
    <?

    echo "\n\n\n<ul id=\"nav_menu\"><!-- Unordered List Start Tag -->\n"

    foreach (
    $main_menu as $key => $keyValue

      echo 
    "\n<li class=\"main_menu\"><a href='"$keyValue['link'] ."'>"$keyValue['name'] ."</a>"
      if(
    is_array($keyValue))
      {
         echo 
    "\n\n<ul class=\"sub_menu\">\n"
         foreach (
    $keyValue as $keyValuex)
         { 
           echo 
    "\n<li><a href='"$keyValuex['link'] ."'>"$keyValuex['name'] ."</a></li>\n"
         }
         echo 
    "</ul><!-- Sub Menu Unordered List Item Closing Tag -->"
       }
       echo
    "\n</li><!-- Sub Menu List Item Closing Tag -->\n"

    echo 
    "\n</ul><!-- Unordered List End Tag -->\n";

  • Users who have thanked poyzn for this post:

    trazix (11-18-2012)

  • #10
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    That new loop gives same result no closing </li> tags


    This is all i have that is to do with main_menu
    PHP Code:
    $main_menu[0] = array('name' => 'HOME PAGE''link' => 'index.php');
    $main_menu[1] = array('name' => 'MENU 2''link' => 'index.php?id=2');
    $main_menu[2] = array('name' => 'MENU 3''link' => 'index.php?id=3');
    $main_menu[3] = array('name' => 'MENU 4''link' => 'index.php?id=4');
    $main_menu[4] = array('name' => 'MENU 5''link' => 'index.php?id=5');

    $main_menu[1][0] = array('name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][1] = array('name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][2] = array('name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][3] = array('name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[1][4] = array('name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx');

    $main_menu[2][0] = array('name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][1] = array('name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][2] = array('name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][3] = array('name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx');
    $main_menu[2][4] = array('name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx'); 
    The H is from the first letter in first array e.g Home Page and the i is from index.php
    This is the list of links as seen on webpage.
    For some reason the loop is creating two Dud links in each menu list It's using the first letter of each array item.
    Code:
    HOME PAGE
    
        H
        i
    
    MENU 2
    
        M
        i
        Sub Menu 1
        Sub Menu 2
        Sub Menu 3
        Sub Menu 4
        Sub Menu 5
    
    MENU 3
    
        M
        i
        Sub Menu 1
        Sub Menu 2
        Sub Menu 3
        Sub Menu 4
        Sub Menu 5
    
    MENU 4
    
        M
        i
    
    MENU 5
    
        M
        i
    Last edited by trazix; 11-18-2012 at 07:58 PM.

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    was not sure if your list was going to have sub-sub menus or not ??
    if it does this is an example to play with.

    PHP Code:
    <? 
    $main_menu
    [] =     array('name' => 'HOME PAGE''link' => 'index.php');
    $main_menu[] = array('name' => 'MENU 2''link' => 'index.php?id=2',
                
    'sub'=>array( 
                    array(
    'name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx'),
                ),
            );
    $main_menu[] = array('name' => 'MENU 3''link' => 'index.php?id=3',
                
    'sub'=>array( 
                    array(
    'name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 4''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 5''link' => 'index.php?id=xxxxxxxxxxx'),
                )
            );
    $main_menu[] = array('name' => 'MENU 4''link' => 'index.php?id=4',
                
    'sub'=>array( 
                    array(
    'name' => 'Sub Menu 1''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 2''link' => 'index.php?id=xxxxxxxxxxx'),
                    array(
    'name' => 'Sub Menu 3''link' => 'index.php?id=xxxxxxxxxxx',
                        
    'sub'=>array(
                            array(
    'name' => 'Sub Sub 1''link' => 'index.php?id=xxxxxxxxxxx'),
                            array(
    'name' => 'Sub Sub 2''link' => 'index.php?id=xxxxxxxxxxx'),
                        )
                    ),
                )
            );
    $main_menu[] = array('name' => 'MENU 5''link' => 'index.php?id=5');


    function 
    mm($main_menu,&$rets){
        
    $rets.= '<ul>';
        foreach(
    $main_menu as $m){
            
    $rets.= '<li>'.$m['name'].'</li>';
            if(
    is_array($m['sub'])){
                
    mm($m['sub'],$rets);
            }
        }
        
    $rets.= '</ul>';
    }

    mm($main_menu,$rets);
    echo 
    $rets;
    Edit:
    hmmmm, works (e.g. displays OK in firefox) but I think my HTML is awry , think should be <li>ul>nested list NOT <li>blah></li><ul>nested list .. etc , will have a play and report back.

    Edit:
    err , think this is 'right' way...

    PHP Code:
    function mm($main_menu,&$rets){
        
    $rets.= '<ul>';
        foreach(
    $main_menu as $m){
            
    $rets.= '<li>'.$m['name'];
            if(
    is_array($m['sub'])){
                
    mm($m['sub'],$rets);
                
    $rets .= '</li>';
            }else{
                
    $rets.='</li>';
            }
        }
        
    $rets.= '</ul>';
    }

    mm($main_menu,$rets);
    echo 
    $rets
    Last edited by firepages; 11-19-2012 at 05:34 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •