How To Fill a HTML Dropdown With Database Results in Code Igniter

How To Fill a HTML Dropdown With Database Results in Code Igniter

In Code Igniter, you can retrieve database results and format the data so that it appears in a HTML dropdown list automatically. Here’s how to do this:

First, you’ll need to edit your model class. Here, you’ll see that we have a method named “get_dropdown()” that will basically query our database table for a set of data, and return it as a id-name array.

class Your_model extends Model {

public function __construct() {
parent::__construct();
}

public function get_dropdown() {
$result = $this->db->select(‘id, name’)->get(‘table_name’)->result_array();

$dropdown = array();
foreach($result as $r) {
$dropdown[$r['id']] = $r['name'];
}
return $dropdown;
}
}

Next, you’ll need to edit your controller class. Your controller needs to load the appropriate model, and then call the method that we’ve created in the model. Finally, we pass the data that the method obtains to our view class.

class Test extends Controller {

public function __construct() {
parent::__construct();

//Load model
$this->load->model(‘your_model’);
}

public function index() {
$data['dropdown'] = $this->your_model->get_dropdown();
$this->load->view(‘your_view’, $data);
}

}

In the view class, we simply just use the following method to generate a dropdown list wherever we want. What this does is to automatically generate the dropdown list with the data that we’ve obtained from the database.

<?php echo form_dropdown(‘your_dropdown’, $dropdown); ?>

, , , , , , , , ,

About Site Fixit!

Sam is a professional web designer and web developer. He has over 15 years of experience with web-related technologies, and loves making things work.

View all posts by Site Fixit!

14 Responses to “How To Fill a HTML Dropdown With Database Results in Code Igniter”

  1. francis Says:

    great, thanks!

    Reply

  2. sansar Says:

    many thanks….very very helpful…many thanks…

    Reply

  3. Rick Says:

    Thank you very much! I was f*cking with it all day!!

    Reply

  4. lavanya Says:

    Thanks. dropdown list is generating. how to get distinct user values in dropdownlist

    Reply

  5. lavanya Says:

    how to get distinct user values in dropdownlist with the above codes..

    $result = $this->db->select(‘id,user’)->get(‘sproject’)->result_array();

    user fields has repeated entries. i need only distinct user values. pls help me

    Reply

    • Site Fixit! Says:

      try

      $result = $this->db->select(‘id,user’)->distinct()->get(‘sproject’)->result_array();

      Reply

      • Lavanya Says:

        Still i get repeated users values… $result = $this->db->select(‘id,user’)->distinct()->get(‘sproject’)->result_array();

        Reply

  6. Lavanya Says:

    or can we populate dropdownlist values instead of having id field.. Pls guide me.. its urgent…

    Reply

  7. Geetika Says:

    I have used your code but i am not getting the result….its showing a page not found error. Is there any need to change in the route.php file. I am a newbie to this please guide me. Also i am not getting what to write at ‘your dropdown’ place.

    Reply

  8. anix Says:

    I am getting variable undefined error in my view. Kindly help me….

    Reply

  9. HPM Says:

    Thank you! this helped me to get the out put successfully!

    Reply

  10. Maji Says:

    THANKS A LOT; YOUR PAGE SAVED ME A LOT OF TIME; AFTER A LOT OF TRY OF SEVERAL CODES; IN YOUR PAGE I SUCCEEDED POPULATING DROP-DOWN MENU; THANKS A LOT!!!!!

    Reply

  11. Ubaid Says:

    thank u so much

    Reply

Leave a Reply