tags:

views:

255

answers:

5

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.

+5  A: 

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 "";
}
solomongaby
+1 Beat me to it. I'll delete mine now.
gclaghorn
+24  A: 

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.

Michał Rudnicki
+1, the proper answer
Daniel
+1 This is a very good option as well and is preferable to either and if/else or a switch in my opinion.
Andrew Hare
This is the proper solution, but this isn't what the question is asking for.
Dustin Fineout
Well if there's a better option to what the question is asking, what's wrong with suggesting that?
Perspx
@Dustin Fineout: That's because the question is wrong. What if he asked 'What's the best way to hit my toes with a ballpen hammer?'
Adriano Varoli Piazza
Nothing, I would just have included both.
Dustin Fineout
The question isn't wrong, he very straightforwardly asked how to do it in ternary, not if it was the best way to do it. Great there are 10 other ways to do it, but seriously, the answer to "how do you write this in ternary" isn't "don't", it's "this is how... but don't"
Dustin Fineout
Thanks for all the answers - didnt know I would touch a nerve quite like that!!! Would like to go with the mapping option, however I cannot figure out how to incorporate it:The Switch function would have to follow $r[0] = because further down the script its echoed in a table - <td id=\"id\"><span id=\"non_sorting_header\">" .$r[0]. "</span></td>....it too far gone to recode the whole thing...this is why ternary worked and nothing else seemed to, any ideas???
@Dustin Fineout: the 'wrongness' of the question is that then I get to see this kind of stuff in production code, by people that don't know better. Especially with PHP.
Adriano Varoli Piazza
+2  A: 

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.

Mark Biek
Doh. Beaten by Michal (http://stackoverflow.com/questions/1069499/convert-if-else-to-ternary/1069540#1069540)
Mark Biek
Good practice, checking for the existence of the key.
Adriano Varoli Piazza
and he even gets a ternary in (correctly, this time)
Adriano Varoli Piazza
+8  A: 

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.

Dustin Fineout
Heh, and now you see why no one was willing to do it.
Chuck
MY EYES!!!!!!!!
Gab Royer
Took 5 seconds in UltraEdit with regex replace. I would never do this myself, but seriously, it isn't that hard to give the answer requested.
Dustin Fineout
Ouch, if I ever come accros something like this it had either be lisp or I'm quiting on the spot and murdering the guy who wrote it.
Unkwntech
Oh. My. GOD. My brain hurts now.
gclaghorn
Sure it isn't hard, but some poor bastard may have to look through this code someday.
SP
I agree wholeheartedly XP
Dustin Fineout
+2  A: 

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.

Jason Creighton