简体   繁体   English

列表框和mysql值

[英]Listbox and mysql value

I am tring to have the selected number showing on the listbox as default. 我希望将选定的号码默认显示在列表框中。

In the follow database i have 在以下数据库中,我有

Database Name: ratings 数据库名称:评分

ID ---------------- Rating ID ----------------评分

1 ---------------- 4 1 ---------------- 4

in my list box i have 1 - 10 i want to be able to show the Rating from the database but i want it to display it as default and when you click on the list box it got 1 - 10 在我的列表框中,我有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>";
      }

Thank you 谢谢

You'll have to have the selected rating recorded someplace. 您必须将选定的评分记录到某个地方。 Once you have that, it's not hard to concatenate a selected='selected' into the 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>";
  }

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

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