2010-10-02 56 views
0

我打算將選定的數字顯示在列表框上作爲默認值。列表框和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

它不是做u想作爲選擇value.However顯示類似期權的任何選擇使用選定的屬性顯示什麼很清楚<期權價值=「X」選擇> ABC – 2010-10-02 00:27:01

回答

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