简体   繁体   English

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

[英]pre-select value in dropdown list from mysql data

I have the following code, which creates a dropdown list based on the contents of a mysql database. 我有以下代码,该代码根据mysql数据库的内容创建一个下拉列表。 I also have a variable $ir1text which is the users current selection from that database. 我还有一个变量$ ir1text,它是用户从该数据库中当前选择的内容。 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>";
?>

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>";
?>

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. 如果您不在同一个表中保留该值,则可以进行另一个查询并存储该值,并将其放在标签的第一个选项中。

If $row[] == $ir1tex show this option selected 如果$row[] == $ir1tex显示此选项

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>";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM