hello,

I don't know whether should I post my problem in this forum or some else. However, is there any method to auto select css tab with corresponding link. I mean to say when ever you open any link from web site its corresponding css tab automatically shows selection(highlighted). I come to know after heavy searching that it can be done through php url recognizing function, but I am not getting any proper method...

please help..

shuja

Dani AI

Generated

A short expert note: ’s page-name extraction is a good starting point and it explains why ’s check worked. For real sites, prefer a small, central "active-nav" rule that normalizes the request and matches by route or path prefix. That handles index/root URLs, query-string pages and pretty URLs, and keeps the same logic across all pages.

Server-side (reliable, recommended): build the menu from a single array and compare a normalized request path. This example matches root, exact paths and prefix-style routes:

<?php
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = rtrim($uri, '/');          // remove trailing slash
if ($uri === '') $uri = '/';      // normalize root

$nav = [
  '/'         => 'Home',
  '/about'    => 'About',
  '/products' => 'Products',
];

foreach ($nav as $path => $label) {
  $isActive = ($uri === $path) || (strpos($uri, $path . '/') === 0);
  $class = $isActive ? ' class="current"' : '';
  echo "<li{$class}><a href=\"{$path}\">{$label}</a></li>\n";
}
?>

Client-side fallback (when server-side is not possible): match link hrefs to location.pathname and add a class to the parent <li>.

<script>
document.querySelectorAll('nav a').forEach(function(a){
  var href = a.getAttribute('href') || '';
  var li = a.closest ? a.closest('li') : a.parentNode;
  if (!li) return;
  if (href === '/' && location.pathname === '/') li.classList.add('current');
  else if (href !== '/' && location.pathname.indexOf(href) === 0) li.classList.add('current');
});
</script>

Troubleshooting notes: normalize trailing slashes and relative vs absolute hrefs; decide whether to match by filename, route name, or prefix (prefix matching is best for "pretty" routes). Avoid echoing raw server strings into HTML without escaping. Keep the nav markup in one included file so the active logic is maintained site-wide.

Recommended Answers

All 4 Replies

hello servis...
the following function will get your page name:

$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
$pagename=$parts[count($parts) - 1];
echo $pagename;

And then, compare your required page name with $page name by using if condition, then make your table get highlighted...
by this:

<? if($pagename=="index.php"){?>
		 <li><a href="index.php" title="css menus" class="current" >Home</a></li>
		<? } else {?>
		 <li><a href="index.php" title="css menus" >Home</a></li>
		 <? }?>

Hope this will help you for something...

thank you Shanti Chepuru, actully, i am requiring something like that....
i will try it and then let you confirm....

yes the code is working very much fine...
please can you explain me the functions on line 2 and 3.

hello...

Actually the $currentFile gives you the whole path of your file ,something like web/mysite/index.php
These two lines

#
$parts = Explode('/', $currentFile);
#
$pagename=$parts[count($parts) - 1];

to get out the pagename from the whole path...
Instead of this two lines code you can simply use this:

basename( $_SERVER["PHP_SELF"]);

Enjoy....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.