December 12, 201015 yr Hi. Recently did a site for a mate and it used php to highlight the current page, a la <li class="link4 <?php if (strpos($_SERVER['PHP_SELF'], 'results.php')) echo 'current';?><?php if (strpos($_SERVER['PHP_SELF'], '/results/')) echo 'current';?>"><a href="/results.php">Results</a></li> The aim was to add a class of "current" to the link on the results.php page and all of the pages within the results directory. I'm sure it worked when I first past the site over to him but looking now it isn't and yet nothing seems to have changed. Quite confused. I've tried changing a few things but no luck however I'm quite a novice with php. Any help is appreciated. Cheers, ~evu.
December 12, 201015 yr Try this: <?php $curPageName=substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); ?> <ul> <li class="link4<?php if($curPageName=='results.php'){echo ' current';}?>"><a href="/results.php">Results</a></li> </ul> It works for me
December 12, 201015 yr Author Do you think that would work with the "/results/" directory? Sorry, haven't a chance to check yet. I should clarify there's no problem with me highlighting the current page, only when I'm trying to highlight all the pages within /results/. Cheers, ~evu.
December 12, 201015 yr Do you think that would work with the "/results/" directory? Sorry, haven't a chance to check yet. I should clarify there's no problem with me highlighting the current page, only when I'm trying to highlight all the pages within /results/. Cheers, ~evu. Oh I understand now... In which case, the answer is I don't honestly know I'm afraid. I expect there are some more experienced php folks on here who can help though
December 13, 201015 yr Author Cheers for the reply nb, just had a go with your script. Works for normal pages, current.php for eg, but not for all pages within /results/ directory unfortunately I'm thinking it's because either script is looking at the end of the url for the *.php or for exact matches. Or it's not including the slashes. I've also tried with * asin /results/* but no luck. Really annoying me now, Google is turning up nothing. Any of the php gurus fancy lending a hand please? Cheers, ~evu.
December 14, 201015 yr Author Mind if I add a lil' bumpage? Anyone at all? Only thing I can think of is removing the php includes and adding the header manually and putting in the class myself. The only problem with that is there's quite a few pages and more to be added in future :/ Cheers, ~evu.
December 14, 201015 yr <li class="link4 <?php if ((strpos($_SERVER['PHP_SELF'], 'results.php') > 0) || (strpos($_SERVER['PHP_SELF'], '/results/')===0) || (strpos($_SERVER['PHP_SELF'], '/results/') > 0)) { echo 'current'; } ?>"><a href="/results.php">Results</a></li> you shouldn't treat strpos as a boolean as it returns an integer or a boolean (false on fail). There is a warning about this on the PHP documentation for strpos. Basically the problem with your code was that it was finding /results/, but it was finding it at position 0, and your if statement was evaluating the integer 0, which it will treat as a boolean, and 0 as a boolean is false.
December 14, 201015 yr Author Major thanks man +1, that fixed it I think I see what you mean now, after reading more of the strpos documents. I had no idea it was working in such a way though, enlightened me Cheers again, ~evu.
Create an account or sign in to comment