I have a dropdown menu created with the code below which fetches all brands from a brands table. It uses a while loop thus showing all on the menu starting alphabetically (i.e., Adiddas and so on). Therefore I don't list them as individual <options>
line by line.
我有一个使用下面的代码创建的下拉菜单,该菜单从品牌表中提取所有品牌。它使用while循环,从而按字母顺序显示菜单上的所有内容(即Adiddas等)。因此,我不会逐行将它们列为单独的
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";
How can I show the sentence "SELECT A BRAND" to appear as the default first value? Should I just enter "SELECT A BRAND" into my brands table and assign a primaryID of zero to it?
如何将“SELECT A BRAND”这个句子显示为默认的第一个值?我应该只在品牌表中输入“SELECT A BRAND”并为其分配一个零的primaryID吗?
Any better way to do this?
有没有更好的方法呢?
All of my searches regarding this question result in topics relating to the 'select=selected attribute'.
我对此问题的所有搜索都会产生与'select = selected属性'相关的主题。
Thanks, Jen
2 个解决方案
#1
0
Just add it manually before the loop:
只需在循环之前手动添加它:
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";
#2
0
Add
echo "<option>SELECT A BRAND</option>";
echo“
as the third line of your code.
作为代码的第三行。
#1
0
Just add it manually before the loop:
只需在循环之前手动添加它:
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";
#2
0
Add
echo "<option>SELECT A BRAND</option>";
echo“
as the third line of your code.
作为代码的第三行。