请告诉我有更好的方法吗?

时间:2011-03-16 19:19:17

标签: php

<label for="state" class="medium">State</label>
<select name="state">
    <option value="" <?php if($_POST['state'] =="") { echo "SELECTED";} ?>>-- Select State --</option>
    <option value="AL" <?php if($_POST['state'] =="AL") { echo "SELECTED";} ?>>Alabama</option>
    <option value="AK" <?php if($_POST['state'] =="AK") { echo "SELECTED";} ?>>Alaska</option>
    <option value="AZ" <?php if($_POST['state'] =="AZ") { echo "SELECTED";} ?>>Arizona</option>
    <option value="AR" <?php if($_POST['state'] =="AR") { echo "SELECTED";} ?>>Arkansas</option>
    <option value="CA" <?php if($_POST['state'] =="CA") { echo "SELECTED";} ?>>California</option>
    <option value="CO" <?php if($_POST['state'] =="CO") { echo "SELECTED";} ?>>Colorado</option>
    <option value="CT" <?php if($_POST['state'] =="CT") { echo "SELECTED";} ?>>Connecticut</option>
    <option value="DE" <?php if($_POST['state'] =="DE") { echo "SELECTED";} ?>>Delaware</option>
    <option value="DC" <?php if($_POST['state'] =="DC") { echo "SELECTED";} ?>>District of Columbia</option>
    <option value="FL" <?php if($_POST['state'] =="FL") { echo "SELECTED";} ?>>Florida</option>
    <option value="GA" <?php if($_POST['state'] =="GA") { echo "SELECTED";} ?>>Georgia</option>
    <option value="HI" <?php if($_POST['state'] =="HI") { echo "SELECTED";} ?>>Hawaii</option>
    <option value="ID" <?php if($_POST['state'] =="ID") { echo "SELECTED";} ?>>Idaho</option>
    <option value="IL" <?php if($_POST['state'] =="IL") { echo "SELECTED";} ?>>Illinois</option>
    <option value="IN" <?php if($_POST['state'] =="IN") { echo "SELECTED";} ?>>Indiana</option>
    <option value="IA" <?php if($_POST['state'] =="IA") { echo "SELECTED";} ?>>Iowa</option>
    <option value="KS" <?php if($_POST['state'] =="KS") { echo "SELECTED";} ?>>Kansas</option>
    <option value="KY" <?php if($_POST['state'] =="KY") { echo "SELECTED";} ?>>Kentucky</option>
    <option value="LA" <?php if($_POST['state'] =="LA") { echo "SELECTED";} ?>>Louisiana</option>
    <option value="ME" <?php if($_POST['state'] =="ME") { echo "SELECTED";} ?>>Maine</option>
    <option value="MD" <?php if($_POST['state'] =="MD") { echo "SELECTED";} ?>>Maryland</option>
    <option value="MA" <?php if($_POST['state'] =="MA") { echo "SELECTED";} ?>>Massachusetts</option>
    <option value="MI" <?php if($_POST['state'] =="MI") { echo "SELECTED";} ?>>Michigan</option>
    <option value="MN" <?php if($_POST['state'] =="MN") { echo "SELECTED";} ?>>Minnesota</option>
    <option value="MS" <?php if($_POST['state'] =="MS") { echo "SELECTED";} ?>>Mississippi</option>
    <option value="MO" <?php if($_POST['state'] =="MO") { echo "SELECTED";} ?>>Missouri</option>
    <option value="MT" <?php if($_POST['state'] =="MT") { echo "SELECTED";} ?>>Montana</option>
    <option value="NE" <?php if($_POST['state'] =="NE") { echo "SELECTED";} ?>>Nebraska</option>
    <option value="NV" <?php if($_POST['state'] =="NV") { echo "SELECTED";} ?>>Nevada</option>
    <option value="NH" <?php if($_POST['state'] =="NH") { echo "SELECTED";} ?>>New Hampshire</option>
    <option value="NJ" <?php if($_POST['state'] =="NJ") { echo "SELECTED";} ?>>New Jersey</option>
    <option value="NM" <?php if($_POST['state'] =="NM") { echo "SELECTED";} ?>>New Mexico</option>
    <option value="NY" <?php if($_POST['state'] =="NY") { echo "SELECTED";} ?>>New York</option>
    <option value="NC" <?php if($_POST['state'] =="NC") { echo "SELECTED";} ?>>North Carolina</option>
    <option value="ND" <?php if($_POST['state'] =="ND") { echo "SELECTED";} ?>>North Dakota</option>
    <option value="OH" <?php if($_POST['state'] =="OH") { echo "SELECTED";} ?>>Ohio</option>
    <option value="OK" <?php if($_POST['state'] =="OK") { echo "SELECTED";} ?>>Oklahoma</option>
    <option value="OR" <?php if($_POST['state'] =="OR") { echo "SELECTED";} ?>>Oregon</option>
    <option value="PA" <?php if($_POST['state'] =="PA") { echo "SELECTED";} ?>>Pennsylvania</option>
    <option value="RI" <?php if($_POST['state'] =="RI") { echo "SELECTED";} ?>>Rhode Island</option>
    <option value="SC" <?php if($_POST['state'] =="SC") { echo "SELECTED";} ?>>South Carolina</option>
    <option value="SD" <?php if($_POST['state'] =="SD") { echo "SELECTED";} ?>>South Dakota</option>
    <option value="TN" <?php if($_POST['state'] =="TN") { echo "SELECTED";} ?>>Tennessee</option>
    <option value="TX" <?php if($_POST['state'] =="TX") { echo "SELECTED";} ?>>Texas</option>
    <option value="UT" <?php if($_POST['state'] =="UT") { echo "SELECTED";} ?>>Utah</option>
    <option value="VT" <?php if($_POST['state'] =="VT") { echo "SELECTED";} ?>>Vermont</option>
    <option value="VA" <?php if($_POST['state'] =="VA") { echo "SELECTED";} ?>>Virginia</option>
    <option value="WA" <?php if($_POST['state'] =="WA") { echo "SELECTED";} ?>>Washington</option>
    <option value="WV" <?php if($_POST['state'] =="WV") { echo "SELECTED";} ?>>West Virginia</option>
    <option value="WI" <?php if($_POST['state'] =="WI") { echo "SELECTED";} ?>>Wisconsin</option>
    <option value="WY" <?php if($_POST['state'] =="WY") { echo "SELECTED";} ?>>Wyoming</option>
</select>

5 个答案:

答案 0 :(得分:3)

$states = array('AL'=>'Alabama','AK'=>'Alaska'...);

foreach($states as $k=>$state){
    echo '<option value="'.$k.'"'.($_POST['state'] == $k ? 'selected="selected"' :''). '>'.$state.'</option>';
}

答案 1 :(得分:2)

那只是在呼喊哈希表。

$States = array("NH" => "New Hampshire", "CA" => "California");

while (list($shortName, $longName) = each($States)) {
    if($_POST['state'] == $shortName) { echo "SELECTED";} echo $longName; ?></option>
}

(不保证确切的语法,这只是很快就写下来了。)

编辑:好的,foreach在这里会更受欢迎:

foreach ($States as $shortName => $longName) {
    if($_POST['state'] == $shortName) { echo "SELECTED";} echo $longName; ?></option>
}

答案 2 :(得分:0)

你可以使用较短的回音ifs(又名tertiary operation):

例如:

<option value="AL" <?php echo (($_POST['state'] =="AL")?"SELECTED":'') ?>>Alabama</option>

以上示例使用:

(boolean)?(if true):(if false)

答案 3 :(得分:0)

为什么不将状态放在数组中,然后循环该数组以构造所有<option>

答案 4 :(得分:0)

这会产生你想要的东西。比EboMike更完整。 (并且我没有调试它。)

<?php $states = array("" => "--Select state--", "AL"=> "Alabama", "AK"=>"Alaska", etc=>etc...); ?>
<label for="state" class="medium">State</label>
<select name="state">
<?php foreach ($i in $states)
      {
          echo "<option value=\"" . $i . "\" ";
          if ($_POST['state'] == $i) //May want to add a strtoupper(...) here
              echo "SELECTED";
          echo ">" . $states[$i] . "</option>\n"
      }
 ?>
 </select>