Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dropdown menu populated by query results. Want one of each, but returning many

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is this possible?

For instance, I have:
Number
Number
Number
Number
Number

My goal:
Number

>Solution :

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>';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading