Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Small problem highlighting current page

Featured Replies

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.

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 :)

  • 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.

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 :)

  • 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.

  • 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.

<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.

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.