Remove duplicates from dropdown list
I am using the below code to return the LastNames list to the form dropdown menu.
<?php
include 'conn.inc.php';
$sql_dropdown_lastname = "SELECT LastName FROM Individuals";
$sql_run_lastname = odbc_exec($conn_general, $sql_dropdown_lastname);
echo "<table><form action='index.php' method='POST'><tr><td>Individual Last Name</td><td><select name='IndivSurname'>";
while($lastname_row = odbc_fetch_array($sql_run_lastname)){
$AllLastName=$lastname_row['LastName'];
echo"<option value='$AllLastName'>$AllLastName</option>";
}
echo"</select></td>
</tr>
<tr>
<td><input type='submit' value='submit' name='submit'></td>
</tr>
</form>
</table>";
?>
However, several entries are duplicated. How can I remove any duplicates from the dropdown?
Thanks in advance, J
+3
source to share
1 answer
If they are indeed duplicates (and not just people with the same last name), you can do it like this:
SELECT DISTINCT LastName FROM Individuals
Note that if they are people who are different from each other but have the same last name, you want to add another field to your query, for example:
SELECT DISTINCT LastName, FirstName FROM Individuals
+5
source to share