rincewind007j Posted November 8, 2008 Share Posted November 8, 2008 i am trying to make a menu which suits the purpose of my website and this is the code i have come up with so far to change a links class as i need each link when active to have the class = current and when inactive to not use a class, i have put 1 link below for your help: <?php print $_SERVER['REQUEST_URI']; ?> <a href="Index.php" <?php if ("Test/Index.php"==$_SERVER['REQUEST_URI'] { class("current"); } else ( class(""); } ?> >Home</a> Link to comment https://forums.phpfreaks.com/topic/131897-trying-to-compare-url-to-one-i-predetermine-to-change-a-links-class/ Share on other sites More sharing options...
thebadbad Posted November 8, 2008 Share Posted November 8, 2008 You should have a variable containing the name or ID of the current page. Then e.g. build the menu with a loop: <?php //$page could be set via the URL, e.g. http://example.com/home/ $page = 'home'; $menu = array( 'home' => 'Home link text', 'about' => 'About link text' ); foreach ($menu as $item => $text) { $current = ($page == $item) ? ' class="current"' : ''; echo "<a href=\"/$item/\"$current>$text</a>\n"; } //<a href="/home/" class="current">Home link text</a> //<a href="/about/">About link text</a> ?> Link to comment https://forums.phpfreaks.com/topic/131897-trying-to-compare-url-to-one-i-predetermine-to-change-a-links-class/#findComment-685229 Share on other sites More sharing options...
rincewind007j Posted November 8, 2008 Author Share Posted November 8, 2008 so by what you put as a reply this will lork like this or would i need to do something else to is as i will be using it as an include on each page? Sorry if it seems like i am asking dumb questions it is just that i am new to php and programming. <?php $page = 'home'; $menu = array( 'home' => 'Home link text', 'login' => 'Login link text', 'forum' => 'Forum link text', 'place 4 kids' => 'Home link text', 'a-z' => 'A-Z link text', 'links' => 'Links link text', 'about' => 'About link text' ); foreach ($menu as $item => $text) { $current = ($page == $item) ? ' class="current"' : ''; echo "<a href=\"/$item/\"$current>$text</a>\n"; } ?> <div class="indentmenu"> <ul> <li> <a href="Index.php">Home</a> </li> <li> <a href="Forgotten Password & Login.php">Login</a> </li> <li> <a href="Forum.php">Forum</a> </li> <li> <a href="Place 4 Kids.php">Place 4 Kidz</a> </li> <li> <a href="A-Z.php">A-Z</a> </li> <li> <a href="Sponsored Links.php">Links</a> </li> <li> <a hred="About.php"></a> </li> </ul> </div> Link to comment https://forums.phpfreaks.com/topic/131897-trying-to-compare-url-to-one-i-predetermine-to-change-a-links-class/#findComment-685646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.