If you want to highlight the active menu, it can be done in a relatively simple way, by identifying the page (assigned to the menu) you are on, by using the PHP_SELF
variable, from which the file name is extracted. Once the page is identified, the “active” class is applied in the menu, appropriately defined in the CSS file.
1 2 3 4 5 6 |
<?php function fc_activ_mnu($pag_activ) { $fis_cur = pathinfo($_SERVER['PHP_SELF'], PATHINFO_BASENAME); if($pag_activ == $fis_cur) { echo 'active'; } } ?> |
So, inside the class attribute, insert the function created above.
1 2 3 4 5 6 7 8 |
<nav class="mnu"> <ul> <li class="<?php fc_activ_mnu('index.php');?>"><a class="" href="index.php">Home</a></li> <li class="<?php fc_activ_mnu('pag01s.php');?>"><a class="" href="pag01s.php">Pag 1</a></li> <li class="<?php fc_activ_mnu('pag02.php');?>"><a href="pag02.php" >Pag 2</a></li> <li class=""><a href="logout.php" >Logout</a></li> </ul> </nav> |
Another way to retrieve the name of the current page, you can also use this variant:
1 2 |
$array_url = explode('/', $_SERVER['REQUEST_URI']); $fis_cur = end($array_url); |
The entire code can also be found at this link.