我试图将所选数字作为默认值显示在列表框中。
在下面的数据库中我有
数据库名称:评级
ID ---------------- 评分
1 ---------------- 4
在我的列表框中,我有 1 - 10,我希望能够显示数据库中的评分,但我希望它显示为默认值,当您单击列表框时,它会显示 1 - 10
$query = "SELECT Rating FROM ratings";
$result = mysql_query($query);
print "<SELECT name=item>";
while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
print "<OPTION value='$value'";
}
print ">$value</OPTION>";
}
谢谢
最佳答案
您必须将所选评级记录在某处。一旦掌握了这一点,将 selected='selected'
连接到选项中就不难了。
$Selected_ID = "4"; // You'll need this.
$query = "SELECT Rating FROM ratings";
$result = mysql_query($query);
print "<SELECT name=item>";
while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
// If the selected ID matches the current row, then mark it as selected.
$Selected = ($Selected_ID == $value) ? " selected='selected'" : '';
print "<OPTION$Selected value='$value'";
}
print ">$value</OPTION>";
}
关于php - 列表框和 mysql 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3843820/