Forum Moderators: coopster

Message Too Old, No Replies

Problem with active url function

         

mr_nabo

7:35 am on Feb 20, 2009 (gmt 0)

10+ Year Member



Hi,

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]

dreamcatcher

9:00 am on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mr_nabo,

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

mr_nabo

9:23 am on Feb 20, 2009 (gmt 0)

10+ Year Member



Hi dreamcatcher,

Thanks for getting back to me - being half-asleep after a long night of work, I missed the ending brace - thanks for pointing that out, it works great now.

Doh!