Better way to code this
Posted: Mon Mar 15, 2010 1:26 am
This is part of a form. If the user makes an error, the form is re-displayed, with whatever values they previously entered. I'm wondering if there is a better way to code what is below. It's part of a drop-down select input. What I've got works, but something tells me there is a better way (fewer lines)...I thought about a switch statement, but that wouldn't be any shorter.
Each option must be listed, and only one should be selected. So each one has two possible states.
Code: Select all
if ($chow == "") {
echo "<option value=\"\" selected></option>\n";
}
if ($chow == "person") {
echo "<option value=\"person\" selected>In Person</option>\n";
} else {
echo "<option value=\"person\">In Person</option>\n";
}//end if
if ($chow == "phone") {
echo "<option value=\"phone\" selected>Telephone Call</option>\n";
} else {
echo "<option value=\"phone\">Telephone Call</option>\n";
}//end if
if ($chow == "email") {
echo "<option value=\"email\" selected>Email</option>\n";
} else {
echo "<option value=\"email\">Email</option>\n";
}//end if
if ($chow == "mail") {
echo "<option value=\"mail\" selected>Mail</option>\n";
} else {
echo "<option value=\"mail\">Mail</option>\n";
}//end if
if ($chow == "txtmessage") {
echo "<option value=\"txtmessage\" selected>Text Message</option>\n";
} else {
echo "<option value=\"txtmessage\">Text Message</option>\n";
}//end if
if ($chow == "other") {
echo "<option value=\"other\" selected>Other</option>\n";
} else {
echo "<option value=\"other\">Other</option>\n";
}//end if