Domanda

I would like to populate a select dropdown with optgroup from the database in codeigniter for that i have written the following:

Table:

+---+-------------+--------------+
|id | group_name  | location_name|
+---+-------------+--------------+
| 1 | top cities  | Bangalore    |
|   |             |              |
| 2 | top cities  | Chennai      |
|   |             |              |
| 3 | big cities  | Mumbai       |
|   |             |              |
| 4 | small cities| mumbai       |
|   |             |              |
| 5 | small cities| mumbai       |
+---+-------------+--------------+

Requirement:

           <select id="location" name="location">
                <option value="" selected="selected">Select Location</option>
                <optgroup label="top cities">
                    <option value="Bangalore">Bangalore</option>
                    <option value="Chennai">Chennai</option>
                </optgroup>
                <optgroup label="big cities">
                    <option value="Mumbai">Mumbai</option>
                </optgroup>
                <optgroup label="small cities">
                    <option value="Mumbai">Mumbai</option>
                    <option value="Mumbai">Mumbai</option>
                </optgroup>
           </select>

Model:

 function get_location() {
    $this->db->select('group_name,GROUP_CONCAT(location_name) AS locations');
    $this->db->order_by("group_name");
    $this->db->from('location');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

View:

            <select id="location" name="location">
                <option value="" selected="selected">Select Location</option>
                <?php
                $CI = & get_instance();
                $CI->load->model('Common', 'Common', TRUE);
                $results = $CI->Common->get_location();
                foreach ($results as $row) {
                    echo '<optgroup label="'.$row->group_name.'">';

                    $location = explode(',', $row->locations);
                    foreach ($location as $loc) {
                        echo '<option value="'.$loc.'">'.$loc.'</option>';
                    }

                    echo "</optgroup>";
                }
                ?>
            </select>

But the outcome is different then I required...

Output:

        <select id="location" name="location">
            <option value="" selected="selected">Select Location</option>
            <optgroup label="top cities">
                <option value="Bangalore">Bangalore</option>
                <option value="Chennai">Chennai</option>
                <option value="Mumbai">Mumbai</option>
                <option value="Mumbai">Mumbai</option>
                <option value="Mumbai">Mumbai</option>
            </optgroup>
       </select> 

Please help anyone... thanks in advance

È stato utile?

Soluzione

It is because your query is wrong. You should add the following line to your query:

    $this->db->group_by('group_name');

Another thing, you shouldn't access your model from your view. You should use the controller to get the data from the model and pass it to the view.

Altri suggerimenti

also try distinct

  $this->db->distinct();

where you place the query

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top