Reputation: 966
I want to be able to generate a drop down list from items entered into a row in a table in SQL. the row is called 'locations'. so lets say, i have the following records:
What would be the best way to query the database to list the different locations in the db (only once for each location).
ie, would return sydney, perth, melbourne.
Thanks.
Upvotes: 1
Views: 88
Reputation: 2228
To just get a list of locations use
select distinct location from yourTableName
You could then build your select by looping through those results.
Or you could alternatively do something like:
select distinct CONCAT("<option>",location,"</option>") from yourTableName
which would remove the need for you to loop through the results to build the options in html, you could just take that result and drop it between your select tags.
Upvotes: 2
Reputation: 185126
If I understand well, try this :
echo "<select>\n";
foreach ($locations as $l) echo "<option>$l</option>\n";
echo "</select>"
Upvotes: 0