I am trying to create a search field that is a dynamically populated drop down menu. The code to query the database and populate the menu is as follows:
<form action="<?=$SERVER['PHP_SELF'];?>" method="post" name="nameSearch">
<label for="nameMenu">
<p>Choose a name from the drop down menu<br />
to retrieve all the data for that person.</p>
</label>
<select name="dateMenu" id="dateMenu">
<option value="0" selected="selected">Choose a name</option>
<?php
$sql_search = 'SELECT id, name from $tbl_name';
$query_search = mysql_query($sql_search);
while($row = mysql_fetch_array($query_search))
{
echo '<option value=' . $row['id'] . '>' . $row['name'] . '</option>';
}
?>
</select>
<input type="submit" name="searchName" value="Find">
</form>
The problem is that because the database holds information that causes it to store the same name repeatedly I get the same name listed as an option in my drop down menu.
The DB looks like so:
----------------------------------------------------------
| id | name | uniqueImage | date | time |
----------------------------------------------------------
| 1 | John | uniqueImage001.png | 2011-03-11 | 14:21:20 |
| 2 | James| uniqueImage002.png | 2011-03-11 | 14:24:30 |
| 3 | Joe | uniqueImage003.png | 2011-03-11 | 14:26:10 |
| 4 | John | uniqueImage004.png | 2011-03-11 | 14:40:10 |
| 5 | Joe | uniqueImage005.png | 2011-03-11 | 14:56:32 |
| 6 | Joe | uniqueImage006.png | 2011-03-11 | 15:02:50 |
| 7 | James| uniqueImage007.png | 2011-03-11 | 15:21:25 |
| 8 | John | uniqueImage008.png | 2011-03-11 | 15:26:30 |
----------------------------------------------------------
and the menu options returned look like so:
<option value="1">John</option>
<option value="2">James</option>
<option value="3">Joe</option>
<option value="4">John</option>
<option value="5">Joe</option>
<option value="6">Joe</option>
<option value="7">James</option>
<option value="8">John</option>
What I need is for the drop down menu to be more like this:
<option value="1">John</option>
<option value="2">James</option>
<option value="3">Joe</option>
This way I can get all of the unique images associated with a particular user. I don't need every name in the DB returned in my query, simply each name only one time. I am thinking I may need to set up a relational DB scheme, but I am trying to avoid this. Is there any other way to set this up? Thanks.
Change your database call to be:
SELECT id, name from $tbl_name group by id, name