nelsok Posted June 28, 2008 Share Posted June 28, 2008 I am going to use if $uri[1] == services then services_class="current" else if $uri[1] == solutions then solutions_class="current" else etc... is there a more efficient way to do this? <?php $uri = explode("/", $_SERVER['REQUEST_URI']); print_r($uri); print($uri[1]); ?> <ul id="nav"> <li><a href="/" class="current">Home</a></li> <li><a href="/services">Services</a></li> <li><a href="/solutions">Solutions</a></li> <li><a href="/technology">Technology</a></li> <li><a href="/quote">Quote</a></li> <li><a href="/company">Company Info</a></li> <li><a href="/contact">Contact Us</a></li> <li><a href="/profile">Work Profile</a></li> </ul> Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 28, 2008 Share Posted June 28, 2008 You could use switch statement. switch(strtolower($uri[1])) { case "services": echo "Services<br />"; break; case "solutions": echo "Services<br />"; break; default: echo "URI was empty<br />"; break; } Is that what you meant or have I misunderstood you? Quote Link to comment Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 I am going to use if $uri[1] == services then services_class="current" else if $uri[1] == solutions then solutions_class="current" else etc... Can you explain that again? Because you just said that you want solution_class="current" for both (and presumably all) conditions, so why not just assign it that to begin with, no conditions at all? But I'm assuming that's not really what you meant. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2008 Share Posted June 28, 2008 I am going to use if $uri[1] == services then services_class="current" else if $uri[1] == solutions then solutions_class="current" else etc... Can you explain that again? Because you just said that you want solution_class="current" for both (and presumably all) conditions, so why not just assign it that to begin with, no conditions at all? But I'm assuming that's not really what you meant. I had to reread it to see the logic as well. Look closely it is "services" class and "solutions" class. eval is a possibility, but I think eval is the lazy, unsecure method in most all scenarios. Quote Link to comment Share on other sites More sharing options...
nelsok Posted June 28, 2008 Author Share Posted June 28, 2008 I am trying to apply class="current" only when the first position of the array is equal to the link location... does that make more sense? so if the current location is /services then class="current" should be applied only to the services link if the current location is /technology then class="current" should be applied only to the technology link Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2008 Share Posted June 28, 2008 Create the links programatically and it will be much easier. <?php $uri = explode("/", $_SERVER['REQUEST_URI']); print_r($uri); print($uri[1]); $linksAry = array ( 'Home' => '', 'Services' => 'services', 'Solutions' => 'solutions', 'Technology' => 'technology', 'Quote' => 'quote', 'Company' => 'company', 'Contact Us' => 'contact', 'Work Profile' => 'profile' ); echo "<ul id=\"nav\">\n"; foreach ($linksAry as $name => $href) { echo "<li><a href=\"$href\" class=\"" . ($uri[1]==$href)?'current':''; . "\">Home</a></li>\n"; } echo "</ul>\n"; ?> [/ode] Quote Link to comment Share on other sites More sharing options...
nelsok Posted June 28, 2008 Author Share Posted June 28, 2008 echo "<li><a href=\"$href\" class=\"" . ($uri[1]==$href)?'current':''; . "\">Home</a></li>\n"; Parse error: syntax error, unexpected '.' should it be replaced or taken out? Quote Link to comment Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 take out the first ; edit: actually i didn't even look you have a ternary in there you can't insert a ternary in there like that. you'll have to put that on a separate line. $x = ($uri[1]==$href)?'current':''; echo "<li><a href=\"$href\" class=\"" . $x . "\">Home</a></li>\n"; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 A ternary operator can be inserted into a string, just you need to wrap it within parenthesis. Example: $n = 9; echo '$n is ' . (($n > 5) ? 'greater' : 'less') . ' than 5'; However it is best to place the ternary operator outside of the string. This allows for the code to be more readable. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2008 Share Posted June 28, 2008 Yes you can use a ternary in a string and, yes, you must enclose it in parens like wildteen88 states. I was trying to post that code right as I was finishing up for the night whic his why I had the errant semi-colon and did not enclse it in parens. I also typically set a variable using the ternary operator before the string. But, I also try to refrain from creating variables on one line if they are only used once on the next line. But, readability is a valid concern. Quote Link to comment Share on other sites More sharing options...
nelsok Posted July 2, 2008 Author Share Posted July 2, 2008 I used this code and replaced "\">Home</a></li>\n"; with "\">$href</a></li>\n"; which returns the array values as the headings but I want to display the array keys. Is there a way to do this? Create the links programatically and it will be much easier. <?php $uri = explode("/", $_SERVER['REQUEST_URI']); print_r($uri); print($uri[1]); $linksAry = array ( 'Home' => '', 'Services' => 'services', 'Solutions' => 'solutions', 'Technology' => 'technology', 'Quote' => 'quote', 'Company' => 'company', 'Contact Us' => 'contact', 'Work Profile' => 'profile' ); echo "<ul id=\"nav\">\n"; foreach ($linksAry as $name => $href) { echo "<li><a href=\"$href\" class=\"" . ($uri[1]==$href)?'current':''; . "\">Home</a></li>\n"; } echo "</ul>\n"; ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted July 2, 2008 Share Posted July 2, 2008 foreach (array as key => value) use $name instead of $href. Quote Link to comment Share on other sites More sharing options...
nelsok Posted July 2, 2008 Author Share Posted July 2, 2008 foreach (array as key => value) use $name instead of $href. thanks. Quote Link to comment 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.