Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

PHP / MYSQL Distinct Dynamic Drop Down Menu

The function below creates a dynamic dropdown menu for which it will select all the cities from each user’s address. If there are duplicate values, they will not be duplicated because DISTINCT was added to the SELECT query. Thus, if there are 7 users and 5 distinct cities, only 5 cities will show up on the dynamic drop down menu.

function dynamic_dropdown() {

echo ‘<select name=”myname”>’;
$command= “SELECT DISTINCT mt.city from mytable as mt where mt.user_id>1 “;
$rows = array();
$result = mysqli_query($db, $command);
if ($result && mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {

//echo $row[‘city’];
$name=$row[‘city’];
echo “<option value=\”$name\” >$name</option>\n”;
}
}
echo ‘</select>’;
}

//call the function
dynamic_dropdown();