$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:[EMAIL PROTECTED]]
> 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:[EMAIL PROTECTED]]
> 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.> 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to