0

I have the following code:

<li>
<label>Manufacturer</label>
<select name="brand">
<?php 
$sql = mysql_query("SELECT CarMake FROM cars");
while ($row = mysql_fetch_array($sql)){
  echo "<option value=\"owner1\">" . $row['CarMake'] . "</option>";
}
?>
</select>

My only issue is the CarMake field has manufacturers listed more than once i.e. BMW is listed more than once so what is happening is obviously BMW is displayed more than once in the dropdown. Is there any way to get the manufacturers to display in alphabetical order and only once each?

Is there a way to add in an Any option to the dropdown so that the user can choose to search by any make rather than a specific one or can this not be done?

3
  • 3
    Use DISTINCT like SELECT DISTINCT CarMake FROM cars Commented Apr 22, 2014 at 16:06
  • ... and an order by clause as well. Commented Apr 22, 2014 at 16:08
  • @MKhalidJunaid Is there a way to add in an Any option to the dropdown so that the user can choose to search by any make rather than a specific one? Also the issue I'm having is that when selecting options from the dropdown box and clicking Search, it's POSTing owner1as the value rather than the actual manufacturer name chosen from the dropdown. How do I resolve this? Commented Apr 24, 2014 at 14:27

2 Answers 2

2
$sql = mysql_query("SELECT DISTINCT CarMake FROM cars ORDER BY CarMake ASC");
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to add in an Any option to the dropdown so that the user can choose to search by any make rather than a specific one?
1
<li>
<label>Manufacturer</label>
<select name="brand">
<?php 
$sql = mysql_query("SELECT distinct CarMake FROM cars order by CarMake");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['CarMake'] . "</option>";
}
?>
</select>

2 Comments

Is there a way to add in an Any option to the dropdown so that the user can choose to search by any make rather than a specific one?
In order to add 'any' option to drop down you will have to put <option value="any">ANY</option>"; right after your <select name="brand">. Then in your code before doing the query, do an if statement that will check the value, and if it is any, it will run select * query, instead it will run your customized one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.