Zola Posted March 6, 2012 Share Posted March 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/ Share on other sites More sharing options...
thegate Posted March 6, 2012 Share Posted March 6, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324523 Share on other sites More sharing options...
marcus Posted March 6, 2012 Share Posted March 6, 2012 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"; Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324532 Share on other sites More sharing options...
Zola Posted March 6, 2012 Author Share Posted March 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324611 Share on other sites More sharing options...
thegate Posted March 6, 2012 Share Posted March 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324622 Share on other sites More sharing options...
Zola Posted March 6, 2012 Author Share Posted March 6, 2012 Yep, absolutely understood. Ill give it a go. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324654 Share on other sites More sharing options...
dannyb785 Posted March 6, 2012 Share Posted March 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258395-navigation-in-php-advice-needed-please/#findComment-1324671 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.