shortysbest Posted April 1, 2010 Share Posted April 1, 2010 I have a php website that i write the navigation and everything in dynamically to each page and i am trying to get it to show the current page you are on (change the css class of the current page link) but i havent been able to figure out how to do this. I would appreciate some help with this. I have tried about everything but im not sure if im just making a tiny error or what is up. Menu Code that is written in dynamically(file named navigation.php):: <li><a href="sw.php?page=home">Home</a></li> <li><a href="sw.php?page=timeline">Timeline</a></li> <li><a href="sw.php?page=countries">Countries</a></li> <li><a href="sw.php?page=photos">Photos</a></li> <li><a href="sw.php?page=facts">Facts</a></li> <li><a href="sw.php?page=summary">Summary</a></li> <li><a href="sw.php?page=memorial">Memorial</a></li> <li><a href="sw.php?page=aftermath">Aftermath</a></li> Menu code that is written dynamically to every page: <ul id="navmenu"> <?php include("navigation.php"); ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/ Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 Is this what you're looking for? <li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li> <li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?> href="sw.php?page=timeline">Timeline</a></li> Etc. Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035435 Share on other sites More sharing options...
shortysbest Posted April 1, 2010 Author Share Posted April 1, 2010 Is this what you're looking for? <li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li> <li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?> href="sw.php?page=timeline">Timeline</a></li> Etc. I put my menu all together with it and it comes back with an error. This i sthe code: <li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li> <li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?> href="sw.php?page=timeline">Timeline</a></li> <li><a<?php echo (($current_page == 'sw.php?page=countries') ? ' class="current_page"' : ''); ?> href="sw.php?page=countries">Countries</a></li> <li><a<?php echo (($current_page == 'sw.php?page=photos') ? ' class="current_page"' : ''); ?> href="sw.php?page=photos">Photos</a></li> <li><a<?php echo (($current_page == 'sw.php?page=facts') ? ' class="current_page"' : ''); ?> href="sw.php?page=facts">Facts</a></li> <li><a<?php echo (($current_page == 'sw.php?page=summary') ? ' class="current_page"' : ''); ?> href="sw.php?page=summary">Summary</a></li> <li><a<?php echo (($current_page == 'sw.php?page=memorial') ? ' class="current_page"' : ''); ?> href="sw.php?page=memorial">Memorial</a></li> <li><a<?php echo (($current_page == 'sw.php?page=aftermath') ? ' class="current_page"' : ''); ?> href="sw.php?page=aftermath">Aftermath</a></li> And for each link it comes back with the error that there is an undefined variable Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035473 Share on other sites More sharing options...
shortysbest Posted April 1, 2010 Author Share Posted April 1, 2010 Actually, the double = signs caused the error, removing those fixed the error, and the links work.. however the function of changing class to current page doesnt work.. Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035480 Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 You have to define $current_page yourself. There's quite a few different ways to do this, and it will depend on what is most efficient for you. One method if executed from sw.php would be: $current_file = basename(__FILE__); $key = NULL; $value = NULL; $get_string = ''; $start = true; foreach ($_GET as $key => $value) { if ($start) { $get_string .= '?'; } else { $get_string .= '&'; $get_string .= $key.'='.$value; } $current_page = $current_file.$get_string; Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035481 Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 Actually, the double = signs caused the error, removing those fixed the error, and the links work.. however the function of changing class to current page doesnt work.. The double equal needs to be in there, = is an assign operator that will always be true when assigning a string to a variable. You should not change the ==. Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035482 Share on other sites More sharing options...
shortysbest Posted April 1, 2010 Author Share Posted April 1, 2010 I'm not having much luck with the code you provided me. lol. sorry im a bit new to php. Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035484 Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 In my haste, I forgot a small detail, apologies, try this instead: $current_file = basename(__FILE__); $key = NULL; $value = NULL; $get_string = ''; $start = TRUE; foreach ($_GET as $key => $value) { if ($start) { $get_string .= '?'; $start = FALSE; } else $get_string .= '&'; $get_string .= $key.'='.$value; } $current_page = $current_file.$get_string; Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035492 Share on other sites More sharing options...
shortysbest Posted April 1, 2010 Author Share Posted April 1, 2010 Well that code doesn't bring back an error now. So thats good i guess, however it's not changing the class of the current page menu button. I put the code in the header.php page which dynamically writes it to all the other page, it should work shouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035494 Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 Well that code doesn't bring back an error now. So thats good i guess, however it's not changing the class of the current page menu button. I put the code in the header.php page which dynamically writes it to all the other page, it should work shouldn't it? No, as I indicated this script must be in the sw.php file (and any other file that the user would see in their browser's navigation bar that needs this functionality). Sorry if I didn't emphasize that more clearly. One method if executed from sw.php would be: Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035497 Share on other sites More sharing options...
shortysbest Posted April 1, 2010 Author Share Posted April 1, 2010 AHHH!..Now it works perfectly!. Thanks so much, this has been bothering me for sometime now.. lol. But thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/197263-dynamic-php-navigation-menu-show-current-page-css/#findComment-1035498 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.