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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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