seowebguy Posted January 26, 2011 Share Posted January 26, 2011 Hi all, my first post here (except my intro post earlier today). I looked for this in the Code Snippet section and didn't see it, and honestly I'm very much a noob at this (for now, at least) so I'm not quite sure what I'd look for anyway. I've got a decent grasp on HTML and CSS and very little PHP. So here's what I need help with: I wrote a simple PHP include so that all my main pages (including the index file) would all use the same file (navtabs.php) for their navigation tabs. But how do I make the code in navtabs.php dynamic for which button should be highlighted on the proper page? Here is the code snippet for my navigation: <nav> <ul> <li><a href="index.php" class="current">Home</a></li> <li><a href="page-two.php" >Page Two</a></li> <li><a href="page-three.php">Page Three</a></li> <li><a href="page-four.php">Page Four</a></li> <li class="last"><a href="page-five.php">Page Five</a></li> </ul> </nav> Where class="current" is the highlighting attribute. All my pages using the same include of this code show the Home tab highlighted? Hoping someone has a simple fix for me. Thanks! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 before the navtabs.php is included, you'll need to set a variable to indicate which page is including the file. for instance, you could set $this_page = 0 for index, $this_page = 1 for page 1, $this_page = 2 for page 2, etc., then, in navtabs.php, you'll check to see whether each nav IS or IS NOT the current page. if it IS the current page, change the CSS class to the active class. if it IS NOT the current page, use the default/inactive CSS class. example: <?php // Before the include, on page 2 for instance: $this_page = 2; // Inside the include file: $active_class = " class='current'"; $default_class = ''; // Initialize all nav classes to default. $nav0class = $nav1class = $nav2class = $nav3class = $nav4class = $nav5class = $default_class; // Set class for selected page ${'nav'.$this_page.'class'} = $active_class; // Display navigation in HTML ?> <nav> <ul> <li><a href="index.php"<?php echo $nav0class; ?>>Home</a></li> <li><a href="page-two.php"<?php echo $nav1class; ?>>Page Two</a></li> <li><a href="page-three.php"<?php echo $nav2class; ?>>Page Three</a></li> <li><a href="page-four.php"<?php echo $nav3class; ?>>Page Four</a></li> <li class="last"><a href="page-five.php"<?php echo $nav4class; ?>>Page Five</a></li> </ul> </nav> Quote Link to comment Share on other sites More sharing options...
seowebguy Posted January 26, 2011 Author Share Posted January 26, 2011 okay well that is still a little over my head mainly because i'm not familiar with the syntax and placement of code. So i would put some of that stuff in the navtabs.php include file? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 yes, everything from // Inside the include file Quote Link to comment 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.