View Full Version : Site Navigation with Selected Feature...Need Help

08-27-2007, 02:58 PM
I have a top navigation on my website.
It is a mouse over type navigation meaning that when you pass your mouse over, the link color changes. However, on the current page, I want the effect to be exactly the same as if it was mouse over. I got this accomplished with css.

Here is the code:

<li id="current"><a href="index.html"><span>Home</span></a></li>
<li><a href="index.html"><span>About Us</span></a></li>
<li><a href="index.html"><span>News</span></a></li>


As you can see, the HOME page is carrying the id, current, meaning it will keep the mouse over effect. However, I want my navigation to change the id to current on which ever page it lands.
I am also using a <?php include('navigation.php');?> file to include that navigation above, meaning it will be an included file.
How would I go about making the current page hold the attribute "current" when the user lands on it, while still using a single include file for all the navigation.

08-27-2007, 04:40 PM
At the top of each page(Home, About, News), before you include the nav.php file, enter this

//'home', 'about' or 'news'
$current = 'home';


Then change navigation.php like so:

<li<?php if($current == 'home') echo ' id="current"';?>><a href="index.html"><span>Home</span></a></li>
<li<?php if($current == 'about') echo ' id="current"';?>><a href="index.html"><span>About Us</span></a></li>
<li<?php if($current == 'news') echo ' id="current"';?>><a href="index.html"><span>News</span></a></li>


08-27-2007, 07:37 PM
You can refine meth's code by setting the script name at the start of each page:

$url = $_SERVER['PHP_SELF'];

You can then use strpos() to check $url for the presence of "index", "about" etc... and set your classes accordingly. This means you don't have to manually specify the page name each time.

Not a big deal with only 3 pages, but worth knowing about.

08-27-2007, 10:18 PM
learn more about reserved variables!! (http://www.php.net/reserved.variables)

08-28-2007, 01:38 AM
I wanted to keep it simple; a beginner and only 3 pages. gc40, personally I use the following at the top of each page:


$script = basename($_SERVER['SCRIPT_NAME']);
$arr_script = explode('.', $script, 2);
$page_name = $arr_script[0];

//site config file e.g:-
include ('./includes/siteConfig.php');


... where setPageVars() is a function with a switch using $page_name for case. The switch will set variables for $title, $meta_desc, $mets_keywords, $page_title and whatever else may be required.

function setPageVars($page) {


case 'index':
$title = 'Index Title';
$meta_desc = 'This is the index page';
$mets_keywords = 'index, page';
$page_title = 'My Index Page';

case 'news':
$title = 'News Title';
$meta_desc = 'This is the news page';
$mets_keywords = 'news, page';
$page_title = 'My News Page';

case 'about_us':
$title = 'About Us';
$meta_desc = 'A little about xyz.com';
$mets_keywords = 'about, xyz.com';
$page_title = 'About XYZ';

$title = 'XYZ.COM';
$meta_desc = 'Welcome to xyz.com';
$mets_keywords = 'widgets, xyz';
$page_title = 'Welcome to xyz.com';



When you include this function in a globally included file, it's a great approach for cutting back on repititious coding.