dongzhao1865 2015-03-20 16:39
浏览 85
已采纳

从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 2015-03-20 18:22
    关注

    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>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?