Show active navigation for current page using php

I am trying to use $current_url = basename($_SERVER['PHP_SELF']); to determine which page I am on, given that my navigation (html) is stored in a php file and is included in each page. This is the code I use to determine which navigation function should be active:

 <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/no-background.css"> <ul> <?php $current_url = basename($_SERVER['PHP_SELF']); $active = "class=\"active\""; ?> <?php if ($current_url == "globaluser.php") { ?> <li <?php echo $active;?> > <?php } ?> <a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li> <?php if ($current_url == "search.php") { ?> <li <?php echo $active;?> > <?php } ?> <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li> <?php if ($current_url == "viewadmins.php") { ?> <li <?php echo $active;?> > <?php } ?> <a href="viewadmins.php?Agent=<?php echo $Agent?>">View admins</a></li> <li class="border-right"><a href="emails.php?Agent=<?php echo $Agent?>">E-mails</a></li> <li class="right border-right"><a href="logout.php"><?php echo $Agent?></a> <ul class="drop1"> <li><a href="earnings.php?Agent=<?php echo $Agent?>">Earnings</a></li> <li id="hover-trigger"><a href="#">Change Password</a> <ul class="drop2"> <li> <form action="changepass.php?Agent=<?php echo $Agent?>" method="POST"> <input type="password" name="1" placeholder="Enter new password"> <input type="password" name="2" placeholder="Repeat new password"> </li> <li> <input type="submit" name="changepw" class="button" value="Change"> </form> </li> </ul> </li> <li> <form action="logout.php"> <input type="submit" class="button" value="Log out"> </form> </li> </ul> </li> 

But that will not work. It shows the active page as active, however it shows 2 pages as simple hyperlinks. It looks like this:

Navigation errors

Does anyone know why this is happening? It works for 2, but not suitable for 2, but the exact code? Thanks

+4
source share
3 answers

You should do it as follows:

 <li <?php echo ($current_url == "globaluser.php") ? $active : ''?> ><a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li> 

because right now you are setting up the opening <li>

+4
source

Are you using <?php } ?> In the wrong place.
This should be after <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li>

The updated code is as follows:

  <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/no-background.css"> <ul> <?php $current_url = basename($_SERVER['PHP_SELF']); echo $current_url; $active = "class=\"active\""; ?> <?php if ($current_url == "globaluser.php") { ?> <li <?php echo $active;?> > <?php } ?> <a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li> <?php if ($current_url == "search.php") { ?> <li <?php echo $active;?> > <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li> <?php } ?> <?php if ($current_url == "viewadmins.php") { ?> <li <?php echo $active;?> > <a href="viewadmins.php?Agent=<?php echo $Agent?>">View admins</a></li><?php } ?> <li class="border-right"><a href="emails.php?Agent=<?php echo $Agent?>">E-mails</a></li> <li class="right border-right"><a href="logout.php"><?php echo $Agent?></a> <ul class="drop1"> <li><a href="earnings.php?Agent=<?php echo $Agent?>">Earnings</a></li> <li id="hover-trigger"><a href="#">Change Password</a> <ul class="drop2"> <li> <form action="changepass.php?Agent=<?php echo $Agent?>" method="POST"> <input type="password" name="1" placeholder="Enter new password"> <input type="password" name="2" placeholder="Repeat new password"> </li> <li> <input type="submit" name="changepw" class="button" value="Change"> </form> </li> </ul> </li> <li> <form action="logout.php"> <input type="submit" class="button" value="Log out"> </form> </li> </ul> </li> 
+1
source

You can also use preg_replace () to add class="active"

 ob_start(); echo '<ul> <li><a href="page1.php">Page 1</a></li> <li><a href="page2.php">Page 2</a></li> </ul>'; $output = ob_get_clean(); $pattern = '~<li><a href="'.$url.'">~'; $replacement = '<li class="active"><a href="'.$url.'">'; echo preg_replace($pattern, $replacement, $output); 
0
source

Source: https://habr.com/ru/post/1497142/


All Articles