Page 1 of 1

Better way to code this

Posted: Mon Mar 15, 2010 1:26 am
by rapathos
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.

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
Each option must be listed, and only one should be selected. So each one has two possible states.

Re: Better way to code this

Posted: Mon Mar 15, 2010 1:33 am
by scarface222
you could just do

Code: Select all

if($variable=="" or $variable2=="" or variable3=="" etc.){
echo"please fill in all fields";
}
As far as a better way, I am not so sure.

Re: Better way to code this

Posted: Mon Mar 15, 2010 4:10 am
by requinix
Put those options in an array, like

Code: Select all

"person" => "In Person"
and try a loop.

Re: Better way to code this

Posted: Mon Mar 15, 2010 4:48 pm
by rapathos
Thank you. That is what I was looking for. I had completely forgotten about using an array. If anyone is interested, the new code went from 33 lines to 12, and is:

Code: Select all

     $howlist = array("person" => "In Person", "phone" => "Telephone Call", "email" => "Email", "mail" => "Mail", "txtmessage" => "Text Message", "other" => "Other");
    
    if ($chow == "") {
        echo "<option value=\"\" selected></option>\n";
    }
    foreach ($howlist  as $key => $value) {
        if ($chow == $key) {
            echo "<option value=\"$key\" selected>$value</option>\n";
        } else {
            echo "<option value=\"$key\">$value</option>\n";
        } //if
    } //foreach