Jump to content

Navigation in PHP - Advice needed please


Zola

Recommended Posts

I am relatively new to PHP, trying to learn more every day.

 

I have been enjoying using Server Side Includes in my sidebars, footer etc with good results, much handier and more efficient!

 

I would like to do the same for the header and main site navigation.

 

However, the main problem that I am not sure how to deal with comes from having 'active' links (differently coloured menu tab for the page the user is on) in my menu.

 

Here is a small menu example:

 

<div class="menu">
                 <ul>
                         <li><a href="nav.php" class="active">Home</a></li>
                         <li><a href="nav2.php">About</a></li>
                         <li><a href="products1.php">Products</a></li>
                         <li><a href="products2.php">Products 2</a></li>
                         <li><a href="products3.php">Products 3</a></li>

                        
                    </ul>
    </div>  

 

When the user is on the Home page, the class is set to active - i.e. a different colour.

 

If I do a simple server side include with my navigation this will of course be across all the pages. Can anyone please advise on what I need to do to make the menu work correctly across all the pages? (have different tabs selected for each respective page).

 

Any help / insight is much appreciated.

Link to comment
Share on other sites

You'll want to give each page a different name.

For example at the top of nav.php you'll write $thispage = 'home';

on top of nav2.php you'll write $thispage = 'about';

etc. (Remember that the $thispage should be above the navigation include, to make sure the variable is set!)

 

Then within the class tag you'll use a if statement

<a href="nav.php" class="<?php if($thispage=="home"){ echo 'active'; }?>">Home</a>
<a href="nav2.php" class="<?php if($thispage=="about"){ echo 'active'; }?>">About</a>

 

Repeat this with every navigation item, and change the value, this way the class will only be active when $thispage is equal to the page name.

 

Edited by ManiacDan: Fixing code to be correct;

Link to comment
Share on other sites

Another way of going about this, if you plan on adding more pages would be using an array to store your page names and check the file name.

 

$pages = array( 'nav.php' => 'Home',
                'nav2.php' => 'About', 
                'products1.php' => 'Products',
                'products2.php' => 'Products 2', 
                'products3.php' => 'Products 3');
                
echo "<div class=\"menu\">\n";
echo "<ul>\n";

$pageName = basename(__FILE__);

foreach($pages AS $url => $name){
    $active = ($url == $pageName) ? ' class="active"' : '';
    
    echo "<li><a href=\"".$url."\"".$active.">".$name."</a></li>\n";
}

echo "</ul></div>\n";

Link to comment
Share on other sites

Thanks for your replys.

 

Thegate - i appreciate your reply, just got a query about it.. Wouldn't  that mean i still have to manually update every page anyway?

 

I like the idea though.

 

Not sure how i would implement the array approach, mcgallforever

Link to comment
Share on other sites

Your post suggested you're making use of includes. Make sure the navigation is posted in an header.php (or seperate navigation file) which is included on every page, this way by editing something to header.php will cause it to apply on all pages.

 

You'll have to edit two files, the header.php file where you'll add a link to the new page, and the new webpage which you will have to give it's own name.

Link to comment
Share on other sites

I'll second what the other guy said, but I'll type the code so it makes more sense

 

home.php

<?php
include("config.php");

$page = "home";
include("header.php");

/blah blah blah
?>

 

header.php

//html code blah blah
<li><a href=#<?php if($page == "home" echo " class='current'; ?>Home</a></li>
<li><a href=#<?php if($page == "about" echo " class='current'; ?>About</a></li>
// etc etc...

// rest of html

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.