RE: [PHP] Better way (if...elseif...else)

From: Date: Fri, 30 Mar 2001 13:26:38 +0000
Subject: RE: [PHP] Better way (if...elseif...else)
Groups: php.general 
Request: Send a blank email to php-general+get-46307@lists.php.net to get a copy of this message
$selected = array(); $selected[$priority] = 'selected'; $pstr .= '<option value="000" '.$selected['000'].'>High'."\n"; $pstr .= '<option value="050" '.$selected['050'].'>Medium'."\n"; $pstr .= '<option value="100" '.$selected['100'].'>Low'."\n"; Is one way of doing that sort of thing, the example below looks a bit difficult to read, but that may just be me seeing as I never use the ternary operator. This solution can also be easily be written as a lopp. I didn't do anything with your $fcol, but that can be solved in a simliar fashion... André Næss > -----Original Message----- > From: Grimes, Dean [mailto:DGRIMES@scvl.com] > Sent: 30. mars 2001 15:17 > To: 'Ashley M. Kirchner'; PHP-General List > Subject: RE: [PHP] Better way (if...elseif...else) > > > $fcol = ($priority == "000") ? "high" : (($prioridy == "050") > ? "med" : > "low"); > $pstr .= sprintf( "<option value=\"000\" %s>High\n". > "<option value=\"050\" %s>Medium\n". > "<option value=\"100\" %s>Low\n", > ($priority == "000") ? "selected" : "", > ($priority == "050") ? "selected" : "", > ($priority == "100") ? "selected" : ""); > > > > That should do the trick!! > > Dean > > -----Original Message----- > From: Ashley M. Kirchner [mailto:ashley@pcraft.com] > Sent: Thursday, March 29, 2001 3:56 PM > To: PHP-General List > Subject: [PHP] Better way (if...elseif...else) > > > > Is there a better way to write the following snippet: > > if ($priority == "000") { > $fcol="high"; > $pstr .= "<option value=\"000\" selected>High\n"; > $pstr .= "<option value=\"050\">Medium\n"; > $pstr .= "<option value=\"100\">Low\n"; > } elseif ($priority == "050") { > $fcol="med"; > $pstr .= "<option value=\"000\">High\n"; > $pstr .= "<option value=\"050\" selected>Medium\n"; > $pstr .= "<option value=\"100\">Low\n"; > } else { > $fcol="low"; > $pstr .= "<option value=\"000\">High\n"; > $pstr .= "<option value=\"050\">Medium\n"; > $pstr .= "<option value=\"100\" selected>Low\n"; > } > > I just hate having to repeat pieces of code. This piece here just > generates a drop down list of items, with the current one being the > selected one.>

« previous php.general (#46307) next »