-1

我有两个 SQL 查询显示来自我的数据库的不同结果。我将这些结果用作导航栏中的导航链接。目前所有结果都显示为单行文本,我想让每个 SQL 查询显示为下拉菜单,查询的所有结果作为下拉菜单中的选项。

这是我正在使用的代码:

<?php

$q = "SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
    echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
    //display product categories
}
mysqli_free_result($result); //free result

$q = "SELECT brand_id, brand_name FROM Brand";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
    echo "<a href='brand.php?id=$row[0]'>$row[1]</a> ";
    //display product Brands
}
?>
4

3 回答 3

1
$q="SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'],$q);
$option1.="<select name='category'>";
while ($row = mysqli_fetch_row($result)){
   $option1.="<option value='$row[0]'>$row[1]</option> ";
}
$option1.="</select>"; 

same for second print this $option1 value in your view file

于 2013-03-13T12:21:08.090 回答
1

利用select option

echo "<select name='category'>";
while ($row = mysqli_fetch_row($result)){
   echo "<option value='$row[0]'>$row[1]</option> ";
}
echo "</select>";
于 2013-03-13T12:19:18.933 回答
0
<select onchange="document.location.href='category.php?id='+this.value;">  
<?php   
    $result = mysqli_query($_SESSION['conn'],$q);
    while ($row = mysqli_fetch_row($result)):?>
    <option value="<?=$row[0];?>"><?=$row[1];?></option>
    <?php endwhile;?>
</select>
于 2013-03-13T12:28:54.460 回答