0

我有一个使用下面的代码创建的下拉菜单,它从品牌表中获取所有品牌。它使用一个while 循环,因此在菜单上按字母顺序显示所有内容(即,阿迪达斯等)。因此,我不会<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”并将primaryID 分配为零吗?

有没有更好的方法来做到这一点?

我对这个问题的所有搜索都会导致与“select=selected 属性”相关的主题。

谢谢,珍

4

2 回答 2

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";
于 2013-03-29T18:43:01.060 回答
0

添加

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

作为代码的第三行。

于 2013-03-29T18:43:42.410 回答