Re: Looking for optimal coding
| From: | LuC . | Date: | Fri, 15 Feb 2002 09:39:45 +0000 |
| Subject: | Re: Looking for optimal coding | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-84878@lists.php.net to get a copy of this message | ||
for($i=1;$i<=50;$i++)
{
if ((!empty($content) && $row[$content] == $states[$i]) || ($dstate == $states[$i]))
$selected="SELECTED";
else
$selected="";
printf("<option
value=\"%s\" %s>%s</option>", $states[$i], $selected, $nstates[$i]);
}
I always prefer to use printf to mix content and data.
And why not use the || your reaching a single solution
Jerry Verhoef
-----Original Message----- From: Joffrey van Wageningen [mailto:joffrey@playcollective.com] Sent: Friday, February 15, 2002 10:00 AM To: PHP Email List Subject: Re: [PHP] Looking for optimal coding This is not a big thing. But I am looking at this thinking there is a way to make the code take up even less lines. for($i=01;$i<=50;$i++) {_________________________________________________________________ Chat on line met vrienden en probeer MSN Messenger uit: http://messenger.msn.nlif (!empty($content)) { if ($row[$content]==$states[$i]) echo "<option value=\"$states[$i]\"selected>$nstates[$i]\n";else echo "<optionvalue=\"$states[$i]\">$nstates[$i]\n";} else { if ($dstate == $states[$i]) echo "<option value=\"$states[$i]\"selected>$nstates[$i]\n";else echo "<optionvalue=\"$states[$i]\">$nstates[$i]\n";}} i would try: for($i=01;$i<=50;$i++) {if(!empty($content) && $row[$content] == $states[$i]) $selected = " selected"; elseif($dstate == $states[$i]) $selected = " selected"; else $selected = ""; echo "<optionvalue=\"".$states[$i]."\"".$selected.">".$nstates[$i]."</option>"; } trading four echo's for a one echo and a extra var and nest the ifs Basically I want to check for two possible conditions to make an item selected. If the first one is valid then do not check for the other. Get what I mean? Any expert programmers out there with the way to chop this even further? test it :) mvgr, Joffrey van Wageningen -- .-[ Joffrey van Wageningen | WoLFjuh | joffrey@ne2000.nl ]-------------- | Networking Event 2000 - www.ne2000.nl - IRCnet:#ne2000, Undernet:#clue | PGP:1024D/C6BA5863 - 3B93 52D3 CB91 9CB7 C50D FA79 865F 628A C6BA 5863 | * We demand guaranteed rigidly defined areas of doubt and uncertainty.| -- Douglas Adams-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php