0

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 :)

0

3 Answers 3

3
<?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.

Sign up to request clarification or add additional context in comments.

2 Comments

Good one, But what happens if i put $currentPage ='faq.php'; ? It will get Array to string conversion
I have updated the code try to re-execute, fixed error.
1

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>

1 Comment

Good, But why you don't consider $currentPage = 'faq.php'; to respect class="current" ?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.