dongzhao1865
dongzhao1865
2015-03-20 16:39

从mysql数据预先选择下拉列表中的值

已采纳

I have the following code, which creates a dropdown list based on the contents of a mysql database. I also have a variable $ir1text which is the users current selection from that database. How can make the drop down list pre-select the value the user already has. The idea being I want to show them what they have and have the dropdown list right there for them to select a new value.

<?php
include("config.php");
$sql1 = "SELECT * FROM iRadios";
$result1 = mysql_query($sql1);

echo "<select name='Station1'>";
while ($row = mysql_fetch_array($result1)) {
    echo "<option value='" . $row['StationName'] ."'>" . $row['StationName'] ."</option>";
}
echo "</select>";
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongwenyou4298 dongwenyou4298 6年前

    Here is the solution

    <?php
    include ("config.php");
    
    $sql1 = "SELECT * FROM iRadios";
    $result1 = mysql_query($sql1);
    echo "<select name='Station1'>";
    while ($row = mysql_fetch_array($result1)) {
        if ($row['StationName'] == trim($ir1text)) {
            echo "<option value='" . $row['StationName'] . "' selected>" . $row['StationName'] . "</option>";
        }
        else {
            echo "<option value='" . $row['StationName'] . "'>" . $row['StationName'] . "</option>";
        }
    }
    echo "</select>";
    ?>
    
    点赞 评论 复制链接分享
  • douzhuo6270 douzhuo6270 6年前

    If $row[]== $ir1tex show this option selected

    while ($row = mysql_fetch_array($result1)) {
        if($row['StationName']==  $ir1text){
            echo "<option value='" . $row['StationName'] .selected"'>" . $row['StationName'] ."</option>";
        }else{
        echo "<option value='" . $row['StationName'] ."'>" . $row['StationName'] ."</option>";
        }
    
    }
    
    点赞 评论 复制链接分享
  • duanao4729 duanao4729 6年前

    Well you can put that user selected value in the first option. if you dont hyave that value in the same table then you can make another query and store that value and put it in the first option of your tag.

    点赞 评论 复制链接分享