php - I have my Dynamic Page, how to add active class on current pages? -


i have 4 php pages, homepage.inc.php, about.inc.php, contact.inc.php , heres index.php

<div id="menu">     <nav>         <a id="home" target="_self" href="index.php">home</a>         <a id="about" target="_self" href="index.php?p=about">about us</a>         <a id="contact" target="_self" href="index.php?p=contact">contact</a>     </nav> </div>  <div id="content">     <?php     $pages_dir = 'pages';     if (!empty($_get['p'])) {         $pages = scandir($pages_dir, 0);         unset($pages[0], $pages[1]);         $p=$_get['p'];           if(in_array($p.'.inc.php', $pages)){             include($pages_dir.'/'.$p.'.inc.php');           }else {          echo 'sorry, page not found.';         }     }else{         include($pages_dir.'/home.inc.php');         }     ?>  </div> 

this css active{background:gray;} want add active class on menu. how?

you can this:

<a id="contact" target="_self" href="index.php?p=contact" <?php if ($_get['p'] == "contact") {     echo  'class="active"';     } ?> >contact</a> 

or using short hand php (making code cleaner)

<a id="contact" target="_self" href="index.php?p=contact"<? (($_get['p']=="contact") ? 'class="active"' : '') ?>>contact</a> 

or can use javascript jquery:

<script> $('#<?php echo $_get['p'] ?>').addclass('active'); </script> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -