logic with arrays

From: Date: Thu, 16 Dec 2004 02:11:02 +0000
Subject: logic with arrays
Groups: php.general 
Request: Send a blank email to php-general+get-204451@lists.php.net to get a copy of this message
I am trying to build a html menu dynamically from data sitting in an array. I have been successful so far until it came to the point of populating the sub-menu of the system. Can someone help me out with the logic or is there a simpler way of doing it? Thanks <?php /** * This function builds the applications menu system from and array of key/pair values * @param null * @return string $menu contains value of menu structure in html */ function BuildMenu() { $base_path = 'http://kangaroo.hotshot/dev/fpaa/DEVELOPMENT/TEST'; $menu_structure = array
   (           array('Home' => '/'),
     array(
            'News' => '/news', 'sub' =>
            array(
                    'Archive' => '?news=archive'
                 )
          ),
     array('Events' => '/events'),
     array('About' => '/about'),
     array('Membership' => '/membership'),
     array('Committees' => '/committee'),
     array('Safety' => '/safety'),
     array(
             'Providers' => '/providers', 'sub' =>
             array(
                    'Sprinklers' => '?providers=sprinklers',
                    'Detection' => '?providers=detection',
                    'Certification' => '?providers=certification',
                    'Monitoring' => '?providers=monitoring',
                    'Hose Reels' => '?providers=hosereels',
                    'Passive' => '?providers=passive',
                    'Portable' => '?providers=portable',
                    'Hazard' => '?providers=hazard',
                    'Maintenance' => '?providers=maintenance',
                    'Consultants' => '?providers=consultants',
                    'Emergency Training' => '?providers=training',
                                      )
          ),
     array('Licencing' => '/licencing'),
     array(
             'Training' => '/training', 'sub' =>
             array(
                   'Tafe Courses' => '?courses=tafe',
                   'Trade Courses' => '?courses=trade',
                   'New Courses' => '?courses=new',
                   'RTO Status' => '?courses=rtostatus',
                   'Portable' => '?courses=portable',
                   'Assessor' => '?courses=assessor',
                   'Training Builetin' => '?courses=builetin',
                   'Contact' => '?courses=contact'                                    )
          ),
     array('Publication' => '/publication'),
     array('Contact' => '/contact'),
); $html_menu = '<ul id="topnav">'; foreach ($menu_structure as $menu_page) {
     $menu_name = array_keys($menu_page);
     $menu_value = array_values($menu_page);
         //$html_menu .= "<li>$menu_name[0] - $menu_value[0]</li>";
          $html_menu .= "<li><a href=\"$base_path$menu_value[0]\">$menu_name[0]</a></li>";
          $sub_menu = sizeof($menu_name);
          if ($sub_menu > 1)
     {
       $html_menu .= '<ul id="subnav">';
       foreach ($menu_page[$menu_name[1]] as $sub_page)
       {
         // Stuck here
         $html_menu .= "<li><a href=\"Need to get value\">$sub_page</a></li>";
       }
       $html_menu .= '</ul>';
     }
        }
      $html_menu .= '</ul>';
      echo $html_menu;
         // Example Structure of html menu
/*
     <ul id="topnav">
     <li><a href="#">Home</a></li>
     <li><a href="#">News</a></li>
     <li><a href="#">Events</a></li>
     <li><a href="#">About</a></li>
     <li><a href="#">Membership</a></li>
     <li><a href="#">Committees</a></li>
     <li><a href="#">Safety</a></li>
     <li class="center"><a href="#" class="here">Providers</a>
       <ul id="subnav">
         <li><a href="#">Sprinklers</a></li>
         <li><a href="#">Detection</a></li>
         <li><a href="#" class="here">Certification</a></li>
         <li><a href="#">Monitoring</a></li>
         <li><a href="#">Hose Reels</a></li>
         <li><a href="#">Passive</a></li>
         <li><a href="#">Portable</a></li>
         <li><a href="#">Hazard</a></li>
         <li><a href="#">Maintenance</a></li>
         <li><a href="#">Consultants</a></li>
         <li><a href="#">Emergency Training</a></li>
       </ul>
     </li>
     <li><a href="#">Licencing</a></li>
     <li><a href="#">Training</a></li>
     <li><a href="#">Publication</a></li>
     <li><a href="#">Contact</a></li>
   </ul>       */
return $menu_structure; } print_r(BuildMenu()); ?>

« previous php.general (#204451) next »