Search code examples
phpmysqlcodeigniteroptgroup

Iterating dropdown list with optgroup in PHP


I am developing an application in CodeIgniter One of my dropdown consists of 86 options which should be generated dynamically and I have decided to make them in <optgroup> as shown in the below screenshot.

enter image description here

I have stored all these options in mysql database. Now my question is I want to iterate these options using foreach() loop so that the Group labels should display for each group. (In the screenshot Education, Hospitality). I don't like the way of hard coding these 86 options. Can someone help me in solving this problem.

Edit

This is my MySQL Table. I have mentioned Education list instead of occupation list. But the table structure is same.

mysql> select * from wededucationlist limit 10;
      +-------+---------------------------+
      | eduid | education                 |
      +-------+---------------------------+
      |     1 | Aeronautical Engineering  |
      |     2 | B Arch                    |
      |     3 | BCA                       |
      |     4 | BE/B-Tech                 |
      |     5 | B Plan                    |
      |     6 | BSc IT / Computer Science |
      |     7 | Other Bachelor Degree     |
      |     8 | M Arch                    |
      |     9 | MCA                       |
      |    10 | ME                        |
      +-------+---------------------------+
      10 rows in set (0.00 sec)

Solution

  • Even if the table you mentioned does not contain the data required, there is a very basic principle of achieving what you want to achieve:

    • Query all Rows that should form the <options> and add a second column, beeing the <optgroup>.

    like this:

    Option 1 | Group 1
    Option 2 | Group 1
    Option 3 | Group 2
    Option 4 | Group 3
    
    • Iterate over all options, and whenever the group changes, print out an optgroup:

    this one:

      <?php
        $data = array(array("option" => 1, "group" => "Group 1"),
           array("option" => 2, "group" => "Group 1"), 
           array("option"=>3, "group" => "Group 2"));
    
        $priorGroup = ""; 
        echo "<select>";
        foreach ($data AS $entry){
          if ($entry["group"] != $priorGroup){ //Start new optgroup if group changed.
             if ($priorGroup != ""){ //close prior optgroup if prior group WAS set.
               echo "</optgroup>"; 
             }
    
             echo "<optgroup label='{$entry["group"]}'>"; 
          }
    
          echo "<option>{$entry["option"]}</option>"; //show option(s)
          $priorGroup = $entry["group"]; //update priorGroup.
        }
    
    
        echo "</optgroup></select>"; //close last optgroup + select.
    ?>
    

    Will output:

     Group 1
        1
        2
     Group 2
        3
    

    Your database query need to sort by group (and optional by option afterwards) otherwhise you might get something like this:

     Group 1
        1
     Group 2
        3
     Group 1
        2