Re: Better way (if...elseif...else)

From: Date: Fri, 30 Mar 2001 10:17:58 +0000
Subject: Re: Better way (if...elseif...else)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46292@lists.php.net to get a copy of this message
"Ashley M. Kirchner" wrote: > > 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"; > } $p_arr=array("000","050","100"); $fc_arr=array("high","med","low"); $ptext_arr("High","Medium","Low"); for($x=0;$x<count($p_arr);$x++) { $pstr.="<option value=\"$p_arr[$x]\" "; if($priority==$p_arr[$x]) { $pstr.="selected"; $fcol=$fc_arr[$x]; } $pstr.=">$ptext_arr[$x]\n"; } You could optimize the code even more if you'd throw $fc_arr away and use $ptext_arr instead. Robert

« previous php.general (#46292) next »