How to disable "current page" link using PHP/Bootstrap CSS

My pages are constructed using PHP/HTML, with Bootstrap CSS and local, custom CSS. I want to generate the code that will automatically detect the “current page” and mark it on the site menu with Bootstrap CSS class “disabled” I have looked high and low in many sites but have not found a solution that doesn’t involve dozens of lines of Javascript, or something similar. I’ve also looked through the auto-suggestions generated on this site as I typed this query, and some came close, but involved additional, unique code on each page.

I’m convinced there should be a simple way to code this, but I can’t figure it out. (I remember that even MS Frontpage could do this many years ago) :wonky:

It seems to me that there should be a way to use something like $_SERVER[‘REQUEST_URI’] as each page loads, compare it to the menu items, and modify the current page menu item link.

Any thoughts or ideas?

Jim

Hi Jim

Try

$page = basename($_SERVER['PHP_SELF']);

and the in your menu, test each page

<a href="tuscany.php"<?php if ($page == 'tuscany.php') echo ' class="disabled"'; ?>>Tuscany</a>

This topic may give you some ideas.

$phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);
$path_parts = pathinfo($phpSelf);
$basename = $path_parts['basename']; // Use this variable for action='':
$pageName = ucfirst($path_parts['filename']);

To make the script a little more secure.

1 Like

Thanks for your replies and suggestions. I’ve looked at them all and in the end came up with a hybrid solution based partly on gandalf458’s approach and the menu setup used by coothead which I found by following the link suggested by SamA74. Because I use folders I ran into some issues that don’t turn up if everything is in one folder.

I placed the following in my common header, called by all pages:
$page = basename($_SERVER['PHP_SELF']);

Here are a few lines from my main menu which is in a php file called by all pages:

<ul id="menu" class="nav nav-stacked">
 <li <?php if ($page == 'bod_current.htm') echo ' class="disabled"'; ?>><a href="https://www.hkvca.ca/bod/documents/bod/bod_current.htm">Executive List</a></li>
etc

The menu php file contains two menus - one is site-wide, the one below it contains links to the “home” page in each folder. I had to give each “home” page a unique name, however. It works fine, although I had to go for absolute links in the site-wide menu (which is OK because they will hardly ever change).

So, thanks to you I’ve learned a lot, and have solved my problem.

All the best,

Jim

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.