Set up a select html element
Inside the element start a php script
Query the database for a list if items - order ascending
Write a loop to insert each value as a html element
Activity: <select name="activity">
<?php
$sql = "select * from activities order by activity_name";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)) {
//form option value //text
echo("<option value=\"".$row["activity_id"]."\">".$row["activity_name"]."</option>");
}
}else{
echo("no records");
}
?>
</select>
In the above example we queried the database and echoed out a select HTML statement using the values from the DB request in a loop to create a dynamic drop down list. Here is an example of a plain HTML Select statement. The advantages are that we can have a nth number of options using a while loop compared to having a static list like below.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>