Reputation:
Please can someone convert the following to ternary for me?
if ($idd == 1521) {
return "Home<br /><img src=\"images/b-value.gif\" /><br />Best for Value";
}
else if ($idd == 1595) {
return "Home<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads";
}
else if ($idd == 1522) {
return "Business<br /><img src=\"images/b-value.gif\" /><br />Best for Value";
}
else if ($idd == 1596) {
return "Business<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads";
}
else if ($idd == 1523) {
return "Voice Product<br /><img src=\"images/vstream200.gif\" /><br />4 Guaranteed Calls";
}
else if ($idd == 1524) {
return "Voice Product<br /><img src=\"images/vstream350.gif\" /><br />7 Guaranteed Calls";
}
else if ($idd == 1525) {
return "Voice Product<br /><img src=\"images/vstream700.gif\"/><br />14 Guaranteed Calls";
}
else
return "";
Thanks.
Upvotes: 0
Views: 1017
Reputation: 22067
Those numbers look like database IDs. If that is the case, a more maintainable solution would be to modify your database schema to store those strings, and then just output the values from the database, instead of trying to switch based on ID.
Upvotes: 2
Reputation: 12939
Ternary operator does not appear to be appropriate in your situation. Why don't you use simple mapping?
$map = array(
1521 => array('Home', 'b-value.gif', 'Best for Value'),
1595 => array('Home', 'b-dload.gif', 'Best for Downloads'),
1522 => array('Business', 'b-value.gif', 'Best for Value'),
// and so on
);
if (array_key_exists($idd, $map)) {
$item = $map[$idd];
echo "{$item[0]} <br/> <img src=\"{$item[1]}\"/> <br/> {$item[2]}";
}
Alternatively, you can pull the map from file or database.
Upvotes: 26
Reputation: 150779
I like to put things like that into an array
$data = array(
"_1521" => "Home<br /><img src=\"images/b-value.gif\" /><br />Best for Value",
"_1595" => "Home<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads",
"_1522" => "Business<br /><img src=\"images/b-value.gif\" /><br />Best for Value"
);
Then you can do your return like this:
return (array_key_exists("_$idd", $data) ? return $data[$idd] : "");
Assuming this whole thing is in a function like this
function getIddString($idd) {
$data = array( /*stuff from above*/);
return (/* stuff from above */);
}
you can then call it whenever and wherever you need to get one of those values.
Upvotes: 2
Reputation: 10534
As nobody else seems willing to just do what you asked, here's the ternary:
return ($idd == 1521
? "Home<br /><img src=\"images/b-value.gif\" /><br />Best for Value"
: ($idd == 1595
? "Home<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads"
: ($idd == 1522
? "Business<br /><img src=\"images/b-value.gif\" /><br />Best for Value"
: ($idd == 1596
? "Business<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads"
: ($idd == 1523
? "Voice Product<br /><img src=\"images/vstream200.gif\" /><br />4 Guaranteed Calls"
: ($idd == 1524
? "Voice Product<br /><img src=\"images/vstream350.gif\" /><br />7 Guaranteed Calls"
: ($idd == 1525
? "Voice Product<br /><img src=\"images/vstream700.gif\"/><br />14 Guaranteed Calls"
: ""
)
)
)
)
)
)
);
But, like everyone else, I suggest you either use a switch or array mapping.
Upvotes: 11
Reputation: 30035
why dont you use a switch ?
switch ($idd) {
case 1521 : return "Home<br /><img src=\"images/b-value.gif\" /><br />Best for Value";
case 1595 : return "Home<br /><img src=\"images/b-dload.gif\"/><br />Best for Downloads";
default: return "";
}
Upvotes: 5