简体   繁体   中英

Need some help in PHP dynamic nested navigation

I have done simple dynamic navigation in PHP like pulling HOME, ABOUT,FAQs, CONTACT from a array a then display it. Now I just need to know what and how can I code if FAQs has 4 more options to diplay after hovering the mouse over it.. I need some help in php coding not in CSS. Currently I'hv this scenario

 <?php
 $pages = array(
   'index.php' => 'Home',
   'cupping.php' => 'Cupping',
   'success.php' => 'Success Stories',
   'healing.php' => 'Healing',
   'eating-right.php' => 'Eeating Right',
   'blog.php' => 'Blog',
   'faq.php' => 'FAQs',
   'contact.php' => 'Contact Us',
 ) ;


<ul class="nav navbar-nav">
   <?php foreach ($pages as $filename => $pageTitle) { 
      if ($filename == $currentPage) { ?>
         <li class="current"><?php echo $pageTitle ; ?></li>
         <?php } else { ?>
       <li><a href="<?php echo $filename ; ?>"><?php echo $pageTitle ; ?></a></li>
            <?php
         } //if 
      } //foreach 
   ?>
</ul>

Now how can I print HEALING that has also some nested navigation so the HTML after php coding look like this

<ul>
 <li><ahref="index.php">Home</a></li>                                    
 <li><a href="cupping.php">Cupping</a></li>                              
 <li><a href="success-stories.php">Success Stories</a></li>                                    
 <li><a href="healing.php">Healing Through</a></li>                                    
  <ul class="submenu">
    <li><a href="herbs.php">Herbs</a></li>                        
    <li><a href="nature.php">Nature</a></li>                                               
    <li><a href="behaviour.php">Behaviour</a></li>                        
  </ul>
 <li><a href="blog.php">Blog</a></li>                                    
 <li><a href="faqs.php">FAQs</a></li>                                    
 <li><a href="contact.php">Contact Us</a></li>                                    
</ul>

I'm new to php and want some help. Thanks :)

<?php
 $pages = array(
   'index.php' => 'Home',
   'cupping.php' => 'Cupping',
   'success.php' => 'Success Stories',
   'healing.php' => 'Healing',
   'eating-right.php' => 'Eeating Right',
   'blog.php' => 'Blog',
   'faq.php' => array('FAQs'=>array('submenu1.php'=>'submenu1','submenu2.php'=>'submenu2')) ,
   'contact.php' => 'Contact Us',
 ) ;


?>

    <ul class="nav navbar-nav">
   <?php foreach ($pages as $filename => $pageTitle) { 
      if ($filename == $currentPage) { 
            if(is_array($pageTitle)){
               foreach ($pageTitle as $menu => $value) {
               echo '<li class="current">'.$menu.'</li>';
               }
            }else{

              echo '<li class="current">'.$pageTitle.'</li>' ;
            }
         ?>


         <?php } else {
            if(is_array($pageTitle)){

               foreach ($pageTitle as $menu => $value) {

                 echo '<li><a href="#">'.$menu.'</a></li>';
                 echo '<ul>';
                     foreach ($value as $key => $submenu) {
                        echo '<li><a href="'.$key.'">'.$submenu.'</a></li>';

                     }
                 echo '</ul>';
               }
            }else{
          ?>
             <li><a href="<?php echo $filename ; ?>"><?php echo $pageTitle ; ?></a></li>
            <?php
            }  
         } //if 
      } //foreach 
   ?>
</ul>

Try this code that will help you to create multiple sub-menu for any main menu.

Try with switching value to index. Please have a look below.

<?php
$pages = array(
    'home' => array('url' => 'index.php', 'caption' => 'Home'),
    'faq' => array('url' => 'faq.php', 'caption' => 'FAQs', 'submenu' =>
        array(
            'submenu1' => array('url' => 'Submenu1.php', 'caption' => 'Submenu1'),
            'submenu2' => array('url' => 'Submenu2.php', 'caption' => 'Submenu2')
        )
    ),
    'contact' => array('url' => 'contact.php', 'caption' => 'Contact Us'),
);
?>
<ul class="nav navbar-nav">
    <?php foreach ($pages as $parent_menu) { ?>
        <li class="<?= ($parent_menu['url'] == $currentPage ? 'current' : '') ?>">
            <a href="<?= $parent_menu['url'] ?>"><?= $parent_menu['caption'] ?></a>
            <?php if (isset($parent_menu['submenu'])) { ?>
                <ul class="">
                    <?php foreach ($parent_menu['submenu'] as $child) { ?>
                        <li><a href="<?= $child['url'] ?>"><?= $child['caption'] ?></a></li>
                    <?php } ?>
                </ul>
            <?php } ?>
        </li>
    <?php } ?>
</ul>

As Disha V. has demonstrated, a multidimensional , associative array is what you want here. Then the nested foreach loop allows you to access the different keys. You can read more about it here: https://www.safaribooksonline.com/library/view/learning-php-5/0596005601/ch04s05.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM