Better way to code this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rapathos
Forum Newbie
Posts: 2
Joined: Mon Mar 15, 2010 1:18 am

Better way to code this

Post 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.
Last edited by rapathos on Mon Mar 15, 2010 1:44 am, edited 1 time in total.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Better way to code this

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Better way to code this

Post by requinix »

Put those options in an array, like

Code: Select all

"person" => "In Person"
and try a loop.
rapathos
Forum Newbie
Posts: 2
Joined: Mon Mar 15, 2010 1:18 am

Re: Better way to code this

Post 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 
Post Reply