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); ?>
April 6, 2012 at 12:40 am
great, thanks!
May 1, 2012 at 5:49 am
many thanks….very very helpful…many thanks…
May 29, 2012 at 9:46 pm
Thank you very much! I was f*cking with it all day!!
February 25, 2015 at 11:37 am
I am still getting error undefined variable. Kindly help me.
July 25, 2012 at 6:43 am
Thanks. dropdown list is generating. how to get distinct user values in dropdownlist
July 25, 2012 at 6:49 am
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
July 25, 2012 at 8:42 am
try
$result = $this->db->select(‘id,user’)->distinct()->get(‘sproject’)->result_array();
July 25, 2012 at 1:07 pm
Still i get repeated users values… $result = $this->db->select(‘id,user’)->distinct()->get(‘sproject’)->result_array();
July 25, 2012 at 1:09 pm
or can we populate dropdownlist values instead of having id field.. Pls guide me.. its urgent…
January 4, 2015 at 3:31 pm
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.
February 25, 2015 at 11:38 am
I am getting variable undefined error in my view. Kindly help me….
June 23, 2015 at 3:36 pm
Thank you! this helped me to get the out put successfully!
October 8, 2015 at 4:40 pm
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!!!!!
May 13, 2017 at 12:09 pm
thank u so much