Hi guys, i want to find something out that will help me out a great deal.

I am including a menu from a separate .php file using

<?php include("menu.php"); ?>

but something that i really need to find out before i can continue with this.

When I'm at eg 'Home' then the 'Home' button should be highlighted. is there a way that with this php include, i can have a script that grabs something within each page, eg a title or a set variable or what ever, and if the content that it grabbed corresponds with the certain button, then it will highlight that button?

here is my menu's html code to show how its being higlited (NOTE: THE GALLERY IS HIGHLIGHTED AND NOT HOME IN THIS CODE)

<div class="nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="menu">
<!---------------------HOME-------------------->
                		<li style="margin-left:5px">
                			<a href="index.php"><span class="l"></span><span class="r"></span>
							<span class="t">Home</span></a>
                		</li>
                		<li>
                			<a href="gallery.php" class=" active"><span class="l"></span><span class="r"></span><span class="t">
                			Gallery</span></a>
                			<ul>
                				<li><a href="gal_catering.php">Catering</a></li>
                				<li><a href="gal_cakes.php">Cakes</a></li>
                				<li><a href="gal_confectionery.php">Confectionery</a>
                			<ul>
								<li><a href="gal_cakes.php">Cookies</a></li>
                				<li><a href="gal_baskets.php">Baskets</a></li>
                			</ul>
                			</li>
                			</ul>
                		</li>
<!------------------------------------------------>		
                		<li>
                			<a href="quote.php"><span class="l"></span><span class="r"></span>
							<span class="t">Quote</span></a>
							<ul>
                				<li><a href="quote_catering.php">Catering</a></li>
                				<li><a href="quote_confectionery.php">Confectionery</a></li>
                			</ul>
                			</li>
<!------------------------------------------------>	
							 <li>
                			<a href="blog/"><span class="l"></span><span class="r"></span>
							<span class="t">Blog</span></a></li>
<!------------------------------------------------>								
                		<li>
                			<a href="about.php"><span class="l"></span><span class="r"></span>
							<span class="t">About</span></a></li>
<!------------------------------------------------>	
                		<li>
                			<a href="contact.php"><span class="l"></span><span class="r"></span>
							<span class="t">Contact</span></a></li>
                	</ul>
                </div>

Dani AI

Generated

Both and offered solid, practical options: a pure‑CSS/body‑id approach keeps presentation in the stylesheet, and the per‑page PHP flag is the simplest for small sites. A slightly more maintainable pattern is to centralize the menu as a single data structure and let the include detect the current request automatically — no per‑page flags, and easy parent/child matching for subpages (e.g. gal_* or /blog/).

Example: build the menu from an array, normalise the request path, then mark the matching item (or parent) as active. This handles root/index, directories, and files with prefixes without changing every page.

$menu = [
  'index'   => ['label'=>'Home','url'=>'/'],
  'gallery' => ['label'=>'Gallery','url'=>'/gallery.php'],
  'quote'   => ['label'=>'Quote','url'=>'/quote.php'],
  'blog'    => ['label'=>'Blog','url'=>'/blog/'],
  'about'   => ['label'=>'About','url'=>'/about.php'],
  'contact' => ['label'=>'Contact','url'=>'/contact.php']
];

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base = basename(rtrim($path, '/'), '.php') ?: 'index';

foreach ($menu as $key => $item) {
  $isActive = ($key === $base) || strpos($path, "/$key/") === 0 || strpos($base, $key . '_') === 0;
  $class = $isActive ? ' class="active"' : '';
  $aria  = $isActive ? ' aria-current="page"' : '';
  echo "<li{$class}><a href=\"{$item['url']}\"{$aria}>" . htmlspecialchars($item['label']) . "</a></li>\n";
}

Notes and tips: prefer styling an .active class in CSS (avoid inline styles), add aria-current="page" for accessibility, escape labels/URLs, and normalise paths to cope with trailing slashes or index files. To debug a missing highlight, view source to confirm the class is output, check CSS specificity (no other rule overriding .active), and confirm the include runs from the same request path (rewrite rules can change what PHP sees). This approach complements both ’s and ’s ideas while keeping the menu DRY and easier to extend.

Recommended Answers

All 3 Replies

On each body tag add an id for that page, ex body id="gallery". Then on each anchor tag add an id for that link, ex: a id="gallery". In your css add body#id a#id foreach link, ex body#gallery a#gallery, body#services a#services{ css styles... }. Since each body will have a unique id and each link has a unique id the css selector will only ever be true when those conditions are met.

Thanks alot, will try it out quickly ^^ appreciate the help!

thanks that worked, i also found another way of doing it using if statements.

add to top of page

<?php 
$page = 'PAGE NAME'; 
?>

then just add to the menu.php infront of the different <li>'s

[B]<?php if ($page == 'PAGE NAME') { ?>[/B]
                		<li class="live" style="margin-left:5px">
                			<a href="index.php" class=" active"><span class="l"></span><span class="r"></span>
							<span class="t">Home</span></a>
                		</li>
                	[B]<?php } else { ?>[/B]
                		<li style="margin-left:5px">
                			<a href="index.php"><span class="l"></span><span class="r"></span>
							<span class="t">Home</span></a>
                		</li>
		        [B]<?php } ?>[/B]

that way if the $page is the same as the certain menu item, it will run the first if else it will run the second code.

Just for others who needs it ;)

but surely 210developer's way would be cleaner, this is just a simpler way for people who don't know what to do in the css template.

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.