我在php中有一个下拉列表,其值来自数据库。在编辑模式中,我需要显示选定的值以进行下拉。我的php在下面:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
答案 0 :(得分:1)
试试这段代码......
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>