我有一个使用下面的代码创建的下拉菜单,该菜单从品牌表中提取所有品牌。它使用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”并为其指定一个零的主要ID吗?
有更好的方法吗?
我对此问题的所有搜索都会产生与'select = selected属性'相关的主题。
谢谢, 仁
答案 0 :(得分: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 :(得分:0)
添加
echo "<option>SELECT A BRAND</option>";
作为代码的第三行。