tomdelonge Posted August 18, 2008 Share Posted August 18, 2008 i have my project set up in sort of "modules". i have one main file that "includes" multiple files. for simplicity, here are the main ones: header.php navigation.php footer.php now in the navigation.php, i need to set a certain anchor to the class "current". i want to use php to do it, what's the best way? i want to leave navigation as one file. is there a simple way to insert php code into the class attribute and echo "current" or "", based upon something? what do other people do? Quote Link to comment https://forums.phpfreaks.com/topic/120134-navigation-with-php/ Share on other sites More sharing options...
haku Posted August 18, 2008 Share Posted August 18, 2008 As you have php output the links, check to see if the href of the link is the same as the filename in the URL. If it is, output a class of current. If it isn't, do nothing. Quote Link to comment https://forums.phpfreaks.com/topic/120134-navigation-with-php/#findComment-618932 Share on other sites More sharing options...
sKunKbad Posted August 18, 2008 Share Posted August 18, 2008 I use the following code to do what you are trying to do. The pages that are included in the list are in the separate file "menuList.inc.php". <?php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } $fullURL = curPageURL(); if(!preg_match("|www|i","$fullURL")){ $parsedURL = parse_url($fullURL); $addWWWURL = $parsedURL['scheme'] . "://www." . $parsedURL['host'] . "/"; } require "menuList.inc.php"; $base=basename($_SERVER['PHP_SELF']); if ($base != 'index.php'){ $menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$base."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$base."\" >$2</a></li>", $menu); }else{ if(preg_match("|www|i","$fullURL")){ $menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$fullURL."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$fullURL."\" >$2</a></li>", $menu); }else{ $menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$addWWWURL."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$addWWWURL."\" >$2</a></li>", $menu); } } $menu=preg_replace("|[\r\n\t]|", "", $menu); echo $menu; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120134-navigation-with-php/#findComment-618954 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.