Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

php - Make an SQL query a drop down menu

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
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use select option

echo "<select name='category'>";
while ($row = mysqli_fetch_row($result)){
   echo "<option value='$row[0]'>$row[1]</option> ";
}
echo "</select>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...