Forum Moderators: coopster
I can't figure out why my function for adding an active link to my navigation isn't working. Can anyone here see the problem?
Thanks:
PHP function
function active($lp_url) {
if($_SERVER['PHP_SELF'] == $lp_url) {
echo 'class="active"';
}
}
HTML
<ul id="nav">
<li><a href="/"<?php
active("/index.php");
}?>>Home</a></li>
<li><a href="/what-we-look-for/"<?php
active("/what-we-look-for/index.php");
}?>>What we look for</a></li>
</ul>
[edited by: phranque at 9:39 am (utc) on Feb. 20, 2009]
Is there any reason why you have the braces in your code? I`m sure they would throw a parse error? Try it without the braces:
<ul id="nav">
<li><a href="/"<?php
active("/index.php");
?>>Home</a></li>
<li><a href="/what-we-look-for/"<?php
active("/what-we-look-for/index.php");
?>>What we look for</a></li>
</ul>
You say its not working. Are you seeing an error? At the top of your PHP page set your error reporting to E_ALL:
error_reporting(E_ALL);
See if that throws any clues. Finally, echo both the values of $_SERVER['PHP_SELF'] & $lp_url to make sure they are the same.
dc