Jump to content

any other way to do this besides a bunch of ifs?


nelsok

Recommended Posts

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>

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?

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 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.

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

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]

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";

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.

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.

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";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.