...

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



gc40
08-27-2007, 01: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:

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

</ul>

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.

meth
08-27-2007, 03: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';

include('navigation.php);



Then change navigation.php like so:




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

</ul>

Pennimus
08-27-2007, 06: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.

rafiki
08-27-2007, 09:18 PM
or
$_SERVER['SCRIPT_FILENAME'];
learn more about reserved variables!! (http://www.php.net/reserved.variables)

meth
08-28-2007, 12: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:

eg


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

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

setPageVars($page_name);



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

switch($page){

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

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

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

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

}//EOS

}//EOF



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum