TomBullock Posted December 8, 2010 Share Posted December 8, 2010 Hi Ive only just started using PHP so go easy I have a PHP include for a menu on a site I run. <body> <html> <ul class="avmenu"> <li><a class="current" href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="services.php">Services</a></li> <li><a href="projects.php">Projects</a> <ul> <li><a href="highcroft_house.php">Highcroft House</a></li> <li><a href="barn_conversion.php">Barn Conversion</a></li> <li><a href="sunroom_extension.php">Sunroom Extension</a></li> <li><a href="dance_school.php">Dance School</a></li> </ul> </li> <li><a href="testimonials.php">Testimonials</a></li> <li><a href="offers.php">Special Offers</a></li> <li><a href="links.php">Links</a></li> <li><a href="contact.php">Contact</a></li> </ul> As you can see, on the first link, the index link, it has the class="current" property which displays a little tab next to the menu to show what link is currently selected. With using the include, obviously the class="current" is passed on to every page. Is there a way that I can tell the include file to change according to what page its on? Any help appreciated! Thanks Tom Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/ Share on other sites More sharing options...
AbraCadaver Posted December 8, 2010 Share Posted December 8, 2010 Off the top of my head, maybe: $page = basename($_SERVER['PHP_SELF']); Then something like this for every link: <?php $class = ($page == 'index.php') ? 'current' : ''; <li><a class="<?php echo $class; ?>" href="index.php">Home</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144524 Share on other sites More sharing options...
JohniMo Posted December 8, 2010 Share Posted December 8, 2010 From a very simple standpoint, in each of your pages just set some variable for the tab number for that page and hit the include: <?php $tab_no=1; include("yourmenu.php"); ?> Then in your menu write out the menu item class (or not) based on the tab number: <?php if ($tab_no=1) { print ("<a class='current' href='mylink1.php'>Link 1</a>"); } else { print ("<a href='mylink1.php'>Link 1</a>"); }; if ($tab_no=2) { print ("<a class='current' href='mylink2.php'>Link 2</a>"); } else { print ("<a href='mylink1.php'>Link 2</a>"); }; if ($tab_no=3) { print ("<a class='current' href='mylink3.php'>Link 3</a>"); } else { print ("<a href='mylink1.php'>Link 3</a>"); }; ?> Pretty rough (and can be simplified greatly), but does that point you in some direction you can follow? J Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144536 Share on other sites More sharing options...
TomBullock Posted December 8, 2010 Author Share Posted December 8, 2010 Off the top of my head, maybe: $page = basename($_SERVER['PHP_SELF']); Then something like this for every link: <?php $class = ($page == 'index.php') ? 'current' : ''; <li><a class="<?php echo $class; ?>" href="index.php">Home</a></li> Ahh, thank you! Worked a treat! Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144710 Share on other sites More sharing options...
TomBullock Posted December 8, 2010 Author Share Posted December 8, 2010 Another issue that has arrised (nothing to do with the above code) is with a footer include. The include code is: <body> <html> <p> Copyright © 2010 <a href="http://www.company.co.uk">Company</a> · Design by <a href="http://www.designer.co.uk">Designer</a></p> When I view this in a browser, it gives me: Copyright © 2010 Company · Design by Designer I remember reading before that I need to put something before the © and the - but Im not sure if thats the correct way? Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144730 Share on other sites More sharing options...
TomBullock Posted December 9, 2010 Author Share Posted December 9, 2010 Managed to sort it! © Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144865 Share on other sites More sharing options...
JohniMo Posted December 9, 2010 Share Posted December 9, 2010 the rest of 'em http://www.w3schools.com/tags/ref_entities.asp Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144980 Share on other sites More sharing options...
Anti-Moronic Posted December 9, 2010 Share Posted December 9, 2010 Another, more manageable way of doing this is to use an array containing the nav items. Then loop through to produce the html. That way, when you add new items you need only update the array, and not copy/paste code all over the place. Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144983 Share on other sites More sharing options...
TomBullock Posted December 9, 2010 Author Share Posted December 9, 2010 the rest of 'em http://www.w3schools.com/tags/ref_entities.asp Awesome! Ill keep a note of that site! Another, more manageable way of doing this is to use an array containing the nav items. Then loop through to produce the html. That way, when you add new items you need only update the array, and not copy/paste code all over the place. Thanks! Ill give it a go Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1144986 Share on other sites More sharing options...
TomBullock Posted October 4, 2011 Author Share Posted October 4, 2011 As a follow up to this, I tried to use the same code method on another site but the class="current" won't work. Here is the code: <body> <html> <?php $page = basename($_SERVER['PHP_SELF']);?> <div class="menu_nav"> <ul> <?php $class = ($page == 'index.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="index.php">Home</a></li> <?php $class = ($page == 'facilities.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="facilities.php">Facilities</a></li> <?php $class = ($page == 'activities.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="activities.php">Activities</a></li> <?php $class = ($page == 'ssp.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="ssp.php">SSP</a></li> <?php $class = ($page == 'bookings.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="bookings.php">Bookings</a></li> <?php $class = ($page == 'contacts.php') ? 'active' : '';?> <li><a class"<?php echo $class; ?>" href="contacts.php">Contacts</a></li> </ul> One thing I did notice is that before adding the inlcude to where the menu would go, the class was set to class="active". I did try changing the all the "current" in the code to "active" but with no joy? Quote Link to comment https://forums.phpfreaks.com/topic/221030-php-include-problem/#findComment-1275681 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.