列表框和mysql值

时间:2010-10-02 00:21:14

标签: php mysql

我想将列表框中显示的所选号码作为默认值。

在以下数据库中我有

数据库名称:评分

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

谢谢

1 个答案:

答案 0 :(得分:1)

您必须在某处记录所选的评分。完成后,将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>";
  }