If I bring sexy back, do I need a receipt?

  • Super Bowl Party at Fiedler's. Peyton, Dungy, and the Colts finally win it.

  • Bill Cosby's incoherent basketball story.
  • Kobe out plays Arenas. Not by much, but the Lakes win. Odom and Parker support with good games. Farmar, where you at?
  • Focus. Concentrate. Learn to love it. This is your spoon.
  • Magic Mountain on Friday, thank God it's not closing .

I hate it when I am happy about something "geeky," but this is one of those times. I was unhappy with the way I was writing a simple little dropdown box to display/update status. Since there are only 3 options, this box was originally hardcoded and was being outputted with echo. This method is far too static and is a true pain in the ass for selecting the current status. The code would look something like this:

echo "
if($order_status == 0){
echo " selected ";
}
echo "value=\"0\">New";

Of course you have to repeat the option 2 more times and set the if statement accordingly. Here comes Ternary to save the day. By creating an array with the statuses:

$statuses = array("New", "Shipped", "Failed");

I now loop through the array and use the Ternary Operator to assign the variable. The new code looks like this:

$selected = ($order_status == $stat) ? " selected " : "";
echo "$stat";

The Ternary took 15 lines of code down to 2. Talk about optimizing code. I encourage you to use Case Selects (Switch) and Ternary as often as possible. Leave the ifs at home for the sake of making your code legible, in case I ever have to read it.

RSS Facebook Status

Archives

Tags

Blogroll

RSS Reddit

RSS Hollywood Tuna

RSS Digg

RSS Stereogum

RSS Egotastic

Spam Blocked