PHP/MySQL Dropdown List
Posted: Tue Jun 01, 2010 12:23 pm
Hi,
I've created a dynamic drop down lit in PHP that draws information from a MySQL database of books. I've created the drop down for users to search the DB using the "catagory" fieldname.
Here's the code:
What's happening is the drop down gets populated with the entire number of categories. The list shows multiple numbers of items on each categories like:
Adult
Adult
Adult
Arts & Crafts
Arts & Crafts
Biography
Business
Business
etc...
How can I have the drop down show only one category item for each category?
Thanks for your help.
I've created a dynamic drop down lit in PHP that draws information from a MySQL database of books. I've created the drop down for users to search the DB using the "catagory" fieldname.
Here's the code:
Code: Select all
<?php
mysql_connect("localhost", "root", "scallywag") or die(mysql_error());
mysql_select_db("books") or die(mysql_error());
$result = mysql_query("SELECT category FROM books ORDER BY category")
or die("Invalid query: " . mysql_query());
echo '<label>Category: </label>';
echo '<select id="category" name="category">';
echo '<option value="">Select</option>';
while ($row = mysql_fetch_assoc($result)) {
$ca = $row['category'];
echo "<option value='$ca'>$ca</option>";
}
echo '</select>';
?>
Adult
Adult
Adult
Arts & Crafts
Arts & Crafts
Biography
Business
Business
etc...
How can I have the drop down show only one category item for each category?
Thanks for your help.