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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器