Re: Looking for optimal coding
| From: | Joffrey van Wageningen | Date: | Fri, 15 Feb 2002 08:59:53 +0000 |
| Subject: | Re: Looking for optimal coding | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-84875@lists.php.net to get a copy of this message | ||
> 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++) {
> if (!empty($content)) {
> if ($row[$content]==$states[$i])
> echo "<option value=\"$states[$i]\"
> selected>$nstates[$i]\n";
> else
> echo "<option
value=\"$states[$i]\">$nstates[$i]\n";
> }
> else {
> if ($dstate == $states[$i])
> echo "<option value=\"$states[$i]\"
> selected>$nstates[$i]\n";
> else
> echo "<option
value=\"$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 "<option
value=\"".$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