Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Site Navigation with Selected Feature...Need Help

    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:
    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.

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    At the top of each page(Home, About, News), before you include the nav.php file, enter this
    PHP Code:

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

    include(
    navigation.php); 
    Then change navigation.php like so:

    Code:
    <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>
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    You can refine meth's code by setting the script name at the start of each page:

    PHP Code:
    $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.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    or
    PHP Code:
    $_SERVER['SCRIPT_FILENAME']; 
    learn more about reserved variables!!

  • #5
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    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
    PHP Code:
    $script basename($_SERVER['SCRIPT_NAME']);
    $arr_script explode('.'$script2);
    $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.

    PHP Code:

    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.
    Last edited by meth; 08-28-2007 at 12:39 AM. Reason: that woulda thrown a parse error
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •