eMonk Posted December 1, 2008 Share Posted December 1, 2008 I'm working with multiple pages and would like to use : <?php include("menu.php"); ?> on all my pages so I won't need to update my css menu bar one by one when changes need to be made. here's part of my css menu : <li><a href="#" title="" class="current">Home</a></li> <li><a href="#" title="">About Us</a></li> <li><a href="#" title="">Services</a></li> <li><a href="#" title="">Our Work</a></li> <li><a href="#" title="">Contact Us</a></li> the tag i want checked through php is the class="current" part. This visually tells the visitor where they are by changing the background color in the menu tab. if the page their on is 'about us' then i would want : <li><a href="#" title="" class="current">About Us</a></li> what's the best way of doing this? $class = 'page name here' at the top of the page then a if/else statement in the menu bar code? Quote Link to comment Share on other sites More sharing options...
MikeDXUNL Posted December 1, 2008 Share Posted December 1, 2008 try <?php $this_file = strrchr($_SERVER['REQUEST_URI'], '/'); $this_file = str_replace('/', '', $this_file); $file = explode("?", basename($this_file)); $this_file = $file[0]; echo '<ul>'; echo '<li><a href="#" title="" '; if($this_file == 'index.php' || $this_file == '') echo 'class="current"'; echo '>Home</a></li> <li><a href="#" title="" '; if($this_file == 'aboutus.php') echo 'class="current"'; echo '>About Us</a></li> <li><a href="#" title="" '; if($this_file == 'services.php') echo 'class="current"'; echo '>Services</a></li> <li><a href="#" title="" '; if($this_file == 'ourwork.php') echo 'class="current"'; echo '>Our Work</a></li> <li><a href="#" title="" '; if($this_file == 'contact.php') echo 'class="current"'; echo '>Contact Us</a></li>'; echo '</ul>'; ?> Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 1, 2008 Share Posted December 1, 2008 Well first you need to have the script figure out what page your on. We can use PHP_SELF $page = $_SERVER['PHP_SELF']; Then we will use IF statements for each menu item <li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li> <li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li> <li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li> <li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li> <li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li> of course your going to need to customize it to you needs. Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 1, 2008 Share Posted December 1, 2008 try <?php $this_file = strrchr($_SERVER['REQUEST_URI'], '/'); $this_file = str_replace('/', '', $this_file); $file = explode("?", basename($this_file)); $this_file = $file[0]; echo '<ul>'; echo '<li><a href="#" title="" '; if($this_file == 'index.php' || $this_file == '') echo 'class="current"'; echo '>Home</a></li> <li><a href="#" title="" '; if($this_file == 'aboutus.php') echo 'class="current"'; echo '>About Us</a></li> <li><a href="#" title="" '; if($this_file == 'services.php') echo 'class="current"'; echo '>Services</a></li> <li><a href="#" title="" '; if($this_file == 'ourwork.php') echo 'class="current"'; echo '>Our Work</a></li> <li><a href="#" title="" '; if($this_file == 'contact.php') echo 'class="current"'; echo '>Contact Us</a></li>'; echo '</ul>'; ?> It is bad practice to use PHP to display HTML. Your Method seems a little too much for this task also. Quote Link to comment Share on other sites More sharing options...
MikeDXUNL Posted December 1, 2008 Share Posted December 1, 2008 i have no clue what I was thinking when I just wrote that up.. REQUEST_URI seemed wrong, but I couldnt remember something easier I guess Quote Link to comment Share on other sites More sharing options...
awpti Posted December 1, 2008 Share Posted December 1, 2008 Ouch, that hurts my head. Here's one I wrote up: <?php class awpnav { function build_links($input_menu, $css_id, $css_id_ns, $uri) { if(!is_array($input_menu)) { return FALSE; } else { foreach($input_menu as $key => $value) { $class = ''; if($uri == str_replace('/', '', $key)) { $class = 'id="'.$css_id.'"'; } elseif($css_id_ns !== '') { $class = 'id="'.$css_id_ns.'"'; } $menu_final[] = '<li '.$class.'><a href="'.$key.'">'.$value.'</a></li>'; } } //Self-explanitory class. Returns a simple array or FALSE return $menu_final; } } Here's how you use it: <?php $menu = array ( '/' => 'Home', '/hosting' => 'Web Hosting', '/services' => 'Services', '/about' => 'About' ); $uri = ....; // Code to figure out the KEY of the array $css_id = 'current'; $css_id_ns = ''; //unselected item has no CSS ID $nav = new awpnav; $result = $nav->build_links($menu, $css_id, $css_id_ns, $uri); Essentially: build_links(array $menu, string $css_id, string $css_id_ns, string $uri); It figures out which menu option to highlight based on the array key. The value of the key is what I actually pass to the page title before rendering the view (content). This needs to be manually edited each time you add a new menu item, but you don't have to add 5-6 lines of HTML+some code. You can see it working here: http://awpti.org/ Quote Link to comment Share on other sites More sharing options...
eMonk Posted December 1, 2008 Author Share Posted December 1, 2008 Well first you need to have the script figure out what page your on. We can use PHP_SELF $page = $_SERVER['PHP_SELF']; Then we will use IF statements for each menu item <li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li> <li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li> <li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li> <li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li> <li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li> of course your going to need to customize it to you needs. result... <li><a href="#" title="" class="current">Home</a></li> <li><a href="#" title="" class="current">About Us</a></li> <li><a href="#" title="" class="current">Services</a></li> <li><a href="#" title="" class="current">Our Work</a></li> <li><a href="#" title="" class="current">Contact Us</a></li> any ideas? Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 1, 2008 Share Posted December 1, 2008 Well first you need to have the script figure out what page your on. We can use PHP_SELF $page = $_SERVER['PHP_SELF']; Then we will use IF statements for each menu item <li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li> <li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li> <li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li> <li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li> <li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li> of course your going to need to customize it to you needs. result... <li><a href="#" title="" class="current">Home</a></li> <li><a href="#" title="" class="current">About Us</a></li> <li><a href="#" title="" class="current">Services</a></li> <li><a href="#" title="" class="current">Our Work</a></li> <li><a href="#" title="" class="current">Contact Us</a></li> any ideas? Forgot double equal sign: <li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li> <li><a href="#" title="" <?php if ($page=='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li> <li><a href="#" title="" <?php if ($page=='services.php'): ?>class="current"<?php endif; ?>>Services</a></li> <li><a href="#" title="" <?php if ($page=='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li> <li><a href="#" title="" <?php if ($page=='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li> Quote Link to comment Share on other sites More sharing options...
eMonk Posted December 1, 2008 Author Share Posted December 1, 2008 thanks for the quick response... none are showing up as class="current" now... i have <?php $page = $_SERVER['PHP_SELF']; ?> at the top of my home.php page.. is this code correct? would this work? <?php $page = 'home.php'; ?> Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 1, 2008 Share Posted December 1, 2008 oh yeah that would work. Quote Link to comment Share on other sites More sharing options...
eMonk Posted December 1, 2008 Author Share Posted December 1, 2008 and it did *spinz on his head* thanks for the support guys! 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.