I included my header in every page which also contains the navigation which leads to the problem on how to add an active class to each nav element if the specific link gets clicked.
I found a working solution on the net.
I put this in my index.php on top:
<?php basename($_SERVER["REQUEST_URI"]) ?>
For each link I do this:
<li><a <?php if (basename($_SERVER["REQUEST_URI"]) == "index.php") echo " class='active' ";?> href="index.php">Start</a></li>
Everything works fine but of course it doesn't work at the very start if you just enter the domain name like:
www.mydomain.com
I tried:
if (basename($_SERVER["REQUEST_URI"]) != " ")
But then it will stick with the active class all the time.
How can I add something like "if it's the root just give the "Start" link and only then the active class?
You could try to use try $_SERVER['PHP_SELF']
instead of $_SERVER["REQUEST_URI"]
ps: if you want to know more about php configuration (including the values of $_SERVER and others) create a php file with <?php phpinfo(); ?>
and call it from browser.
You can use
if($_SERVER['REQUEST_URI'] == '/')
which tells you are in root path.
function current_page_name()
{
$url = explode('/',$_SERVER['PHP_SELF']);
$current_url = $url[count($url)-1];
return $current_url;
}
This function return your current page.
Hope this will help you.