设置基于MySQL值选择的下拉列表

时间:2012-09-28 17:28:00

标签: php mysql

  

可能重复:
  an option already selected on page load in drop down list and also changeble

我有一个下拉列表:

<select name="province">
                <option value="Alberta">Alberta</option>
                <option value="British Columba">British Columba</option>
                <option value="Manitoba">Manitoba</option>
                <option value="New Brunswick">New Brunswick</option>
                <option value="Newfoundland">Newfoundland</option>
                <option value="N.W. Territories">N.W. Territories</option>
                <option value="Nova Scotia">Nova Scotia</option>
                <option value="Ontario">Ontario</option>
                <option value="Prince Edward Island">Prince Edward Island</option>
                <option value="Saskatchewan">Saskatchewan</option>
                <option value="Yukon Territory">Yukon Territory</option>
                <option value="Quebec">Quebec</option>
</select>

表单将选择提交到MySQL数据库。这是个人资料页面。当用户返回此配置文件页面以更改选择时,他的选择(从MySQL中获取)应显示为已选中。任何想法如何实现这一目标?

即。再次加载配置文件页面时,请检查数据库,获取字段,将其与列表进行比较,然后选择该值作为选定值。

2 个答案:

答案 0 :(得分:1)

你有正确的逻辑,就这么做!

  <?php
        $province_array = array('Alberta', 'British Columba', ......, 'Quebec');

        echo '<select name="province">';
        //get the user's setting with user_id from DB. You should get this before this script.
        $sql = "SELECT user_province FROM the_table WHERE user_id=$user_id;";
        $query = mysql_query($sql, $the_connection) or die (mysql_error());
        if($row = mysql_fetch_assoc($query)) {
          $user_province = $row['user_province']; 
           foreach($province_array as $value) {
         if ($value == $user_province) { //if the province==the user's setting, make it default 
           echo '<option value="'.$value.'" selected="selected">'.$value.'</option>';
         } else { //else, echo it as regular
           echo '<option value="'.$value.'">'.$value.'</option>';
         }
       }
    }
    echo '</select>';
    ?>
  1. 建立与数据库的连接。
  2. 确保您可以获得类似“user_id”的内容 区分不同的用户省和数据库。
  3. 然后是样本。

答案 1 :(得分:0)

以下是我要做的事情:

在显示表单之前初始化变量。

if ($_SERVER['REQUEST_METHOD'] == "POST") {  // if we're getting info from the form
    $defaultProvince = $_POST['province'];    // set the province to the selected pulldown
} else {
    $defaultProvince = "";                   // otherwise default to an empty string
}

$provinces = ("Alberta",                     // set up the provinces array
              "British Columbia",
              "Manitoba",
              "New Brunswick",
              ....
              );

然后在表单显示例程中我会添加:

echo '<select name="province">\n';
foreach ($provinces as $province) {         // run through our array of provinces
    echo '   <option value="$province"';
    if ($province === $defaultProvince) {   // if the default province matches this particular array province
                                            // then we'll select it
         echo ' selected="selected"'        // xhtml 1.0 Strict/1.1 compliant
    }
    echo '>$province</option>\n';
}
echo '</select>\n';