Rickstar
Rickstar

Reputation: 6189

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

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

$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

Upvotes: 0

Views: 1311

Answers (1)

Mahdi.Montgomery
Mahdi.Montgomery

Reputation: 2024

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

Upvotes: 1

Related Questions