Home > Software design >  Dropdown menu populated by query results. Want one of each, but returning many
Dropdown menu populated by query results. Want one of each, but returning many

Time:04-29

I have populated a dropdown menu with my query results using Mysqli

echo '<select>';
   echo '<option>Semester</option>';
   $q = "SELECT semester_id FROM semOffered";
   $result = mysqli_query($dbc, $q);
   while($row = mysqli_fetch_array($result)) {
       echo '<option>' . $row['semester_id'] . '</option>';
   }
   echo '</select>';

$dbc is my database connection

Within my semester_id column I have repeating values. I would like to only display one of these values as a representative of the many.

Is this possible?

For instance, I have: Number Number Number Number Number

My goal: Number

CodePudding user response:

try this array_unique() to remove duplicate elements or values in an array.

echo '<select>';
   echo '<option>Semester</option>';
   $q = "SELECT semester_id FROM semOffered";
   $result = mysqli_query($dbc, $q);
   $result = array_unique($result)
   while($row = mysqli_fetch_array($result)) {
       echo '<option>' . $row['semester_id'] . '</option>';
   }
   echo '</select>';
  • Related