Guest the nerd herd Posted June 13, 2010 Share Posted June 13, 2010 Hello, I've a project where I have to be able to code so that the current page, the link to that is a diffrient color than the rest. In example, "link1, link2, link3, link4, link5" I go to link1, and the link changes color, and stays that way as long as I am on the page, I go to link2, that changes and link1 changes back to normal, I managed to do this, but through some annoying php code that took quite a while, I'm considering if there's some smart guys around that has any knowledge on how to do this in css/html, I tried messing around with A:active but it would only change color as I pressed the link. My site is set up with: if(isset($_GET['page'])) { $page = $_GET['page']; include("includes/".$page.".php"); }So it's the _same_ menu on all the sub-pages, so I'm not able to just go change some classes on the menu, is there a way to do this? Here's the way I set it up now, and as you can see for yourself, there must be an easier way to do this?<?php define('THIS_PAGE', $_GET['page']);echo '<ul>';if (THIS_PAGE == 'videos') echo '<li><a href="index.php?page=videos"><font color="#000">Videos</font></a></li>';else echo '<li><a href="index.php?page=videos"><font color="#FFF">Videos</font></a></li>';if (THIS_PAGE == 'images') echo '<li><a href="index.php?page=images"><font color="#000">images</font></a></li>';else echo '<li><a href="index.php?page=images"><font color="#FFF">images</font></a></li>';if (THIS_PAGE == 'Tour Dates') echo '<li><a href="index.php?page=images"><font color="#000">Tour Dates</font></a></li>';else echo '<li><a href="index.php?page=images"><font color="#FFF">Tour Dates</font></a></li>';if (THIS_PAGE == 'Tour Dates') echo '<li><a href="index.php?page=images"><font color="#000">Tour Dates</font></a></li>';else echo '<li><a href="index.php?page=images"><font color="#FFF">Tour Dates</font></a></li>';if (THIS_PAGE == 'News') echo '<li><a href="index.php?page=images"><font color="#000">News</font></a></li>';else echo '<li><a href="index.php?page=images"><font color="#FFF">News</font></a></li>';if (THIS_PAGE == 'Discography') echo '<li><a href="index.php?page=images"><font color="#000">Discography</font></a></li>';else echo '<li><a href="index.php?page=images"><font color="#FFF">Discography</font></a></li>';echo '</ul>';?> Thanks for reading Link to comment Share on other sites More sharing options...
dsonesuk Posted June 13, 2010 Share Posted June 13, 2010 you could store link values in an array, and just loop through them to change link color to current pageif(isset($_GET['page'])){define("THIS_PAGE", $_GET['page']);}else{define("THIS_PAGE", "videos"); //sets default active link at start}$linkarray = array("videos","images","tour-dates","news","discography");echo '<ul id="nav">';for($i=0;$i<count($linkarray);$i++){if (THIS_PAGE == $linkarray[$i]) echo '<li><a href="index.php?page='.$linkarray[$i].'"><font color="#000">'.ucwords(str_replace("-"," ",$linkarray[$i])).'</font></a></li>';else echo '<li><a href="index.php?page='.$linkarray[$i].'"><font color="#FFF">'.ucwords(str_replace("-"," ",$linkarray[$i])).'</font></a></li>';}echo '</ul>';better still! because using <font> is old school, set a class for anchor links related to active and non active<style type="text/css">#nav {background-color:#66CC00;}#nav a {color:white;} /*default color for all states*/#nav a.active {color:black;}</style>if(isset($_GET['page'])){define("THIS_PAGE", $_GET['page']);}else{define("THIS_PAGE", "videos"); //sets default active link at start} $linkarray = array("videos","images","tour-dates","news","discography"); echo '<ul id="nav">'; for($i=0;$i<count($linkarray);$i++) { if (THIS_PAGE == $linkarray[$i]) echo '<li><a href="index.php?page='.$linkarray[$i].'" class="active">'.ucwords(str_replace("-"," ",$linkarray[$i])).'</a></li>'; else echo '<li><a href="index.php?page='.$linkarray[$i].'">'.ucwords(str_replace("-"," ",$linkarray[$i])).'</a></li>'; } echo '</ul>'; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.