2013-03-29 47 views
0

我有一個下面的代碼創建的下拉菜單,該代碼從品牌表中提取所有品牌。它使用一個while循環,從而在菜單上按字母順序顯示所有內容(即Adiddas等)。因此,我不會將它們逐行列爲單獨的<options>在下拉菜單中顯示「選擇一個...」

echo "<form action=\"type.INC.php\" method=\"get\">\n"; 
echo "<select name=\"brand\">\n"; 

$stmt = mysqli_stmt_init($hook); 
if($stmt=mysqli_prepare($hook,"SELECT brandid, brandname FROM brands WHERE brandid ")); 
{ 
mysqli_stmt_bind_param($stmt,"i", $brandid); 
mysqli_stmt_execute($stmt); 
mysqli_stmt_bind_result($stmt, $brandid, $brandname); 
while(mysqli_stmt_fetch($stmt)) 
    { 
    $brandname = htmlspecialchars($brandname, ENT_QUOTES, 'UTF-8'); 
    echo "<option value=\"$brandid\">$brandname </option>"; 
    }  
echo "</select>\n"; 
echo "<input name=\"submit\" type=\"submit\" id=\"brandid\" value=\"submit\" />\n"; 
echo "</form> \n"; 

如何顯示句子「SELECT A BRAND」作爲默認第一個值出現?我是否應該在品牌表中輸入「SELECT A BRAND」併爲其分配一個零標識號?

有沒有更好的方法來做到這一點?

我對這個問題的所有搜索都會產生與'select = selected attribute'相關的主題。

感謝, 仁

回答

0

只是在循環之前手動添加:

echo "<form action=\"type.INC.php\" method=\"get\">\n"; 
echo "<select name=\"brand\">\n"; 
// Select a brand, empty value: 
echo "<option value=\"\">(SELECT A BRAND)</option>"; 

$stmt = mysqli_stmt_init($hook); 
if($stmt=mysqli_prepare($hook,"SELECT brandid, brandname FROM brands WHERE brandid ")); 
{ 
    mysqli_stmt_bind_param($stmt,"i", $brandid); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_bind_result($stmt, $brandid, $brandname); 
    while(mysqli_stmt_fetch($stmt)) 
    { 
     $brandname = htmlspecialchars($brandname, ENT_QUOTES, 'UTF-8'); 
     echo "<option value=\"$brandid\">$brandname </option>"; 
    }  
    echo "</select>\n"; 
    echo "<input name=\"submit\" type=\"submit\" id=\"brandid\" value=\"submit\" />\n"; 
    echo "</form> \n"; 
+1

很酷!這工作。謝謝,亞當! – Jen

0

添加

echo "<option>SELECT A BRAND</option>";

爲你的代碼的第三行。

相關問題