I have this simple PHP code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("boxcar", $con);
echo "<form>";
echo "<select>";
echo "<option>--Select Brand--</option>";
$result = mysql_query("SELECT * FROM cars");
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['Brand'].">".$row["Brand"]."</option>";
}
echo "</select><br/><br/>";
echo "<input type='button' name='submit' value='Search'/>"
echo "</form>";
mysql_close($con);
?>
I’m new at this and just trying to build a advanced search function for variety of car models by the use of drop-down menus. I just want to have every car brand name as a parent category show only once on the dropdown list. I’m currently showing duplicated brand names because I have different car models with the same brand name.
In addition to that, I would like to have the 2nd dropdown menu for car models show the list of car models that correspond to the selected parent category(car brand) only and do not show all car models from database.
I currently have this code and the car models dropdown list show all models from database:
//--------Get Car Brands----------
$result = mysql_query("SELECT DISTINCT cars.brand FROM cars ORDER BY brand ASC");
while($row=mysql_fetch_array($result)){
echo "<option value=".$row['brand'].">".$row["brand"]."</option>";}
echo "</select> <select class='model'>";
//--------Get Car Models----------
echo "<option>--Select Model--</option>";
$result = mysql_query("SELECT DISTINCT cars.model FROM cars ORDER BY model ASC");
while($row=mysql_fetch_array($result)){
echo "<option value=".$row['model'].">".$row["model"]."</option>";}
echo "</select><br/><br/>";
Try using
SELECT * FROM car GROUP BY car.Brandor
SELECT DISTINCT car.Brand FROM car;