0

I've got a wee problem. I know from answers such as this how to have an active link on the navigation bar.

However, this doesn't work if there are GET variables after the link. So while my <li> gets class = "active" if I load daily.php, it doesn't get the class if I load daily.php?project=4.

The code I am using right now for the navigation bar and making a certain link active is this:

<?php
function echoActiveClassIfRequestMatches($requestUri)
{
    $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");

    if ($current_file_name == $requestUri)
        echo 'class="active"';
}
?>

<div id="menu">
<ul>
<li <?php echoActiveClassIfRequestMatches("daily") ?>><a href="daily.php">Daily</a></li>
<li <?php echoActiveClassIfRequestMatches("weekly") ?>><a href="weekly.php">Weekly</a></li>
<li <?php echoActiveClassIfRequestMatches("monthly") ?>><a href="monthly.php">Monthly</a></li>
</ul></div>

As I said, this doesn't work if instead of daily.php I load daily.php?project=4.

All help is welcome, and if you need any more code, please just ask!

Thank you very much.

Community
  • 1
  • 1
Jose Salvatierra
  • 2,407
  • 6
  • 21
  • 41

2 Answers2

3

You can use strpos rather than "==". It will check if the URL contains the address you wanted. Or you can split the URL using explode

$current_url = explode("?", $_SERVER['REQUEST_URI']);
echo $current_url[0] ;
user3309301
  • 301
  • 1
  • 4
1

Here is a some modified function

<?php
function echoActiveClassIfRequestMatches($requestUri)
{       
    $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");

    if (strpos($current_file_name,$requestUri)!==false)
        echo 'class="active"';
}
?>
Pvb
  • 446
  • 2
  • 5