attila2452 Posted July 25, 2009 Share Posted July 25, 2009 How can i shorten this code to make it smaller or more condense.this is my navigation to my website.here is the link to what it looks like and what it does.http://attilahajzer.comli.com/ <?php if($_GET["page"] == ""){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=welcome'><font color='#87CEEB'> Home </font></a></li>";} elseif($_GET["page"] === "welcome"){echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=welcome'><font color='#87CEEB'> Home </font></a></li>";} else{echo"<li><a href='?page=welcome'> Home </a></li>";} if($_GET["page"] === "about"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=about'><font color='#87CEEB'> About </font></a></li>";} else{echo"<li><a href='?page=about'> About </a></li>";} if($_GET["page"] === "media"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>";} elseif($_GET["page"] === "music"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }elseif($_GET["page"] === "images"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }elseif($_GET["page"] === "images2"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }elseif($_GET["page"] === "albums"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }elseif($_GET["page"] === "links"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }elseif($_GET["page"] === "widgets"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=media'><font color='#87CEEB'> Media </font></a></li>"; }else{ echo"<li><a href='?page=media'> Media </a></li>"; } if($_GET["page"] === "downloads"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=downloads'><font color='#87CEEB'> Downloads </font></a></li>"; } else{ echo"<li><a href='?page=downloads'> Downloads </a></li>"; } if($_GET["page"] === "contact"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=contact'><font color='#87CEEB'> Contact </font></a></li>"; }else{ echo"<li><a href='?page=contact'> Contact </a></li>"; } if($_GET["page"] === "guestbook"){ echo"<li style='background-image: url(images/nav_hov.jpg);'><a href='?page=guestbook'><font color='#87CEEB'> Guestbook </a></li>"; }else{ echo"<li><a href='?page=guestbook'> Guestbook </a></li>"; }?> Link to comment Share on other sites More sharing options...
morrisjohnny Posted July 25, 2009 Share Posted July 25, 2009 This question was answered in your other Thread? and you said it was working if i remember correctly? Link to comment Share on other sites More sharing options...
attila2452 Posted July 25, 2009 Author Share Posted July 25, 2009 This question was answered in your other Thread? and you said it was working if i remember correctly? this is different because this is my navigation before that was my title. my navigation has a background. and i just don't want to scew it up. and because i am rellly new with PHP. i rarely know what im reading./ so i have to go over it again and again. and make comments saying what is doing what (from my understanding)you know what i mean? Link to comment Share on other sites More sharing options...
Ingolme Posted July 26, 2009 Share Posted July 26, 2009 Try using the switch() statement. Link to comment Share on other sites More sharing options...
gekk0 Posted July 27, 2009 Share Posted July 27, 2009 Ok, well, i'd make an an array with your links and their corresponding $_GET variable. Then run this array in a foreach() loop, checking with each array element if it equals the $_GET['page'] variable, if it does, then when the link html is created it should have the selected class assigned to it... heres the code, i might not of explained that veeery well, but hopefully you'll see. You can add more links this way by modifying the $links array <style type="text/css"><!--.selectedList {background-image: url(images/nav_hov.jpg);}.selectedLink {color:#87CEEB;}.unselectedList {}.unselectedLink {}--!></style><?phpfunction buildMenu() { $links = array( array( 'urlVar' => 'welcome', 'title' => 'Home'), array( 'urlVar' => 'about', 'title' => 'About'), array( 'urlVar' => 'media', 'title' => 'Media'), array( 'urlVar' => 'downloads', 'title' => 'Downloads'), array( 'urlVar' => 'contact', 'title' => 'Contact'), array( 'urlVar' => 'guestbook', 'title' => 'Guestbook'), ); $build = '<ul>' . "\n"; foreach($links as $link) { if($_GET['page'] == $link['urlVar']) { $class = 'selected'; } else { $class = 'unselected'; } $build .= ' <li class=' . $class . 'List> <a href="?page=' . $link['urlVar'] . '" class="' . $class . 'Link"> ' . $link['title'] . ' </a> </li>' . "\n"; } $build .= '</ul>' . "\n"; return $build; }echo buildMenu();?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.