Re: [PHP] Better way (if...elseif...else)
"Ashley M. Kirchner" wrote: Is there a better way to write the following snippet: if ($priority == "000") { $fcol="high"; $pstr .= "option value=\"000\" selectedHigh\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\" selectedMedium\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\" selectedLow\n"; } $p_arr=array("000","050","100"); $fc_arr=array("high","med","low"); $ptext_arr("High","Medium","Low"); for($x=0;$xcount($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 -- 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]
RE: [PHP] Better way (if...elseif...else)
$fcol = ($priority == "000") ? "high" : (($prioridy == "050") ? "med" : "low"); $pstr .= sprintf( "option value=\"000\" %sHigh\n". "option value=\"050\" %sMedium\n". "option value=\"100\" %sLow\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\" selectedHigh\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\" selectedMedium\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\" selectedLow\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. AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- 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] -- 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]
RE: [PHP] Better way (if...elseif...else)
$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 Nss -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\" %sHigh\n". "option value=\"050\" %sMedium\n". "option value=\"100\" %sLow\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\" selectedHigh\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\" selectedMedium\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\" selectedLow\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]
Re: [PHP] Better way (if...elseif...else)
$p_arr=array("000","050","100"); $fc_arr=array("high","med","low"); $ptext_arr("High","Medium","Low"); for($x=0;$xcount($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"; } Even better! This looks like to winning entry for our mini-programming contest. =) Sig for a Day Stephan Ahonen, ICQ 491101 "That's very funny Scotty, now beam down my clothes!" Come back tomorrow for a different sig! Backspace a single "s" to reply by email -- 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]
RE: [PHP] Better way (if...elseif...else)
One thing I would do different is: switch ($fcol){ case 000: break; case 050: break; default: break; } To me the switch/case code is easier to read/maintain. I know that doesn't actually solve your repetition problem but it will make it a little easier to look at. John Guynn This email brought to you by RFCs 821 and 1225. -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED]] Is there a better way to write the following snippet: if ($priority == "000") { $fcol="high"; $pstr .= "option value=\"000\" selectedHigh\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\" selectedMedium\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\" selectedLow\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]
Re: [PHP] Better way (if...elseif...else)
you can use switch statements: switch($priority){ case 000: code here break; case med: more code break; default: in case none match use default } -jack "Ashley M. Kirchner" wrote: Is there a better way to write the following snippet: if ($priority == "000") { $fcol="high"; $pstr .= "option value=\"000\" selectedHigh\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\" selectedMedium\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\" selectedLow\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. AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- 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] -- 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]
RE: [PHP] Better way (if...elseif...else)
-Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 29, 2001 1: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\" selectedHigh\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\" selectedMedium\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\" selectedLow\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. $val = array("000","050","100"); $val_name = array("Low","Medium","High"); $count = count($val); $counter = 0; while ($counter $count) { if ($val[$counter] == $priority) { $selected = " selected"; } echo "option value=\"$val[$counter]\"$selected$val_name[$counter]/option"; $counter++; } -- 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]
RE: [PHP] Better way (if...elseif...else)
-Original Message- From: Morgan Curley [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 29, 2001 2:52 PM To: PHP-General List Subject: RE: [PHP] Better way (if...elseif...else) Make sure you reset $selected at the end of the while loop. OoPs! Yes, reset $selected, haha. if HTML readability is a concern you can also do something like the following( for static lists I find it easier this way ): ?php $priority_selected[$priority] = 'SELECTED'; ? SELECT NAME="priority" OPTION VALUE="000" ?php echo($priority_selected['000'])?Low OPTION VALUE="050" ?php echo($priority_selected['050'])?Medium OPTION VALUE="100" ?php echo($priority_selected['100'])?High /SELECT morgan At 05:30 PM 3/29/2001 -0800, Jason Lotito wrote: -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 29, 2001 1: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\" selectedHigh\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\" selectedMedium\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\" selectedLow\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. $val = array("000","050","100"); $val_name = array("Low","Medium","High"); $count = count($val); $counter = 0; while ($counter $count) { if ($val[$counter] == $priority) { $selected = " selected"; } echo "option value=\"$val[$counter]\"$selected$val_name[$counter]/option"; $counter++; } -- 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] -- 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]
Re: [PHP] Better way (if...elseif...else)
$val = array("000","050","100"); $val_name = array("Low","Medium","High"); $count = count($val); $counter = 0; while ($counter $count) { if ($val[$counter] == $priority) { $selected = " selected"; } echo "option value=\"$val[$counter]\"$selected$val_name[$counter]/option"; $counter++; } Not quite... You need to reset $selected so that you don't end up with them all being selected when $priority="000". It's a pretty clever idea, though. I've rearranged stuff a bit to make it look prettier, and to match the original functionality closer. Nothing personal, but I can't stand it when people use while for stuff that looks prettier as a for. =) $val = array("000","050","100"); $val_name = array("High","Medium","Low"); $fcol_name = array("high","med","low"); $count = count($val); for ($counter = 0; $counter $count; $counter++) { $selected = ""; if ($val[$counter] == $priority) { $selected = " selected"; $fcol = fcol_name[$counter]; } //if $pstr .= "option value=\"$val[$counter]\"$selected$val_name[$counter]/option\r\n"; } //while Sig for a Day Stephan Ahonen, ICQ 491101 "That's very funny Scotty, now beam down my clothes!" Come back tomorrow for a different sig! Backspace a single "s" to reply by email -- 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]
RE: [PHP] Better way (if...elseif...else)
-Original Message- From: Stephan Ahonen [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 29, 2001 5:20 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Better way (if...elseif...else) $val = array("000","050","100"); $val_name = array("Low","Medium","High"); $count = count($val); $counter = 0; while ($counter $count) { if ($val[$counter] == $priority) { $selected = " selected"; } echo "option value=\"$val[$counter]\"$selected$val_name[$counter]/option"; $counter++; } Not quite... You need to reset $selected so that you don't end up with them all being selected when $priority="000". It's a pretty clever idea, though. Yeah, someone mentioned that after I posted it, then I took a shotgun to my foot..haha. Yeah, well, hey, I release that code as a Beta Version anyways! =) Yes, good ole' for, though I have been so inundated with while() that it just comes naturally. Hey, at least I don't printf() people to death! =) I've rearranged stuff a bit to make it look prettier, and to match the original functionality closer. Nothing personal, but I can't stand it when people use while for stuff that looks prettier as a for. =) $val = array("000","050","100"); $val_name = array("High","Medium","Low"); $fcol_name = array("high","med","low"); $count = count($val); for ($counter = 0; $counter $count; $counter++) { $selected = ""; if ($val[$counter] == $priority) { $selected = " selected"; $fcol = fcol_name[$counter]; } //if $pstr .= "option value=\"$val[$counter]\"$selected$val_name[$counter]/option\r\n"; } //while Sig for a Day Stephan Ahonen, ICQ 491101 -- 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]