Load menu and sub-menus from two MySQL queries PHP -


                      $menu = mysql_query(" query 1");                       $k = 1;                       ($s = 0; $s < mysql_num_rows($menu); $s++)                         {                           $menus= mysql_fetch_assoc($menu);                           $mainmenu[]=$menus['name'];                     $submenus=mysql_query("query 2");                    ($m = 0; $m < mysql_num_rows($submenus); $m++)                       {                $submenu = mysql_fetch_assoc($submenus);                 $subitem[]=$submenu['name'];                   $url=$submenu['url'];                      }                 }                   foreach($mainmenu  $mains)                 {              echo '<li class="hasul"><a><span><b>' .$mains.'</b></span></a></li>';              foreach($subitem $sub)                  {                echo '<ul>';                echo '<li><a href="md5($url);=request"><span>' .$sub. '</span></a></li>';                echo '</ul>';               }                 }  

the code above show 2 queries load menu , submenus. query2 uses inputs query1. menus load correctly contain same menu items. ideally each menu should have own menu items.

you have foreach inside foreach, means, every main menu outputing all submenus.

create parameter submenu, like:

$mainmenu = array(         1 => 'about',         2 => 'news',         3 => 'search',     );  $submenu = array(         1 => array( 'about name', 'about location' ),         3 => array( 'search me' ),     ); 

and check submenu need:

if ( is_array( $mainmenu ) ) {     echo '<ul>';     foreach( $mainmenu $key=>$menu )     {         echo '<li>'.$menu.'</li>';         if ( is_array( $submenu[$key]) )         {             echo '<ul>';             foreach( $submenu[$key] $sub )             {                 echo '<li>'.$sub.'</li>';             }             echo '</ul>';         }     }     echo '</ul>'; } 

this produce:

<ul>     <li>about</li>     <ul>         <li>about name</li>         <li>about location</li>     </ul>     <li>news</li>     <li>search</li>     <ul>         <li>search me</li>     </ul> </ul> 

i hope idea.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -