做一个SQL查询下拉菜单 [英] Make an SQL query a drop down menu

查看:260
本文介绍了做一个SQL查询下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个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>;
//显示产品类别
}
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>;
//显示产品品牌
}
?>


解决方案

  $ 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>;

对于第二个
打印此视图文件中的$ option1值


I have two SQL queries that display different results from my database. I'm using these results as navigation links in my nav bar. At the moment all the results display as a single line of text, I want to make each SQL query display as a drop down menu, with all the results from the query as an option in the drop down.

Here's the code I'm using:

<?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
}
?>

解决方案

$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

这篇关于做一个SQL查询下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆