How to populate drop-down input from database in Codeigniter

Maangatech > Web Design > How to populate drop-down input from database in Codeigniter


Codeigniter is a powerful and lightweight framework for developing web applications. As a web developer sometimes you need to add dropdown inputs in your web application form; but furthermore you like dropdown inputs that are populated with data from the database.

In this tutorial, I will guide you through step by step toward adding this type of input in your application. Remember in this tutorial I will not cover form validation and processing, this will be covered in another article.

Codeigniter Framework

  1. I hope you have created a database and connected it with your Codeigniter application. If not yet do it; you can follow this guide.
  2. Create a table named “job_positions” and populate it; use the snippet below.
-- Table structure for table `job_positions` 
CREATE TABLE IF NOT EXISTS `job_positions` ( 
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `job_position` varchar(200) DEFAULT NULL, 
    `description` varchar(200) DEFAULT NULL, 
    `added_date` date NOT NULL, 
    `updated_on` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
-- Dumping data for table `job_positions` 
INSERT INTO `job_positions` (`id`, `job_position`, `description`, `added_date`, `updated_on`) VALUES 
(1, 'Customer care', 'Dealing with customer care', '2016-06-01', '0000-00-00 00:00:00'), 
(2, 'IT Staff', 'Dealing with ICT', '2015-12-02', '2015-12-02 00:00:00'), 
(3, 'Bursar', 'Dealing with financial activities', '2015-12-02', '2015-12-11 00:00:00'), 
(4, 'Sales Officer', 'Dealing with Sales tasks', '2016-06-01', '0000-00-00 00:00:00'); 
-- --------------------------------------------------------
  1. Create a Model named Job_positions_model with the following codes. This will help us to query data from the DB also I will add the selected value of input which is “Please select position…”
< ? php 
class Job_positions_model extends CI_Model { 
    function __construct() { 
    //get job positions from the db 
    function get_job_positions() { 
        $result = $this - > db - > select('id, job_position') - > get('job_positions') - > result_array(); 
        $job_position = array(); 
        foreach($result as $r) { 
            $job_position[$r['id']] = $r['job_position']; 
        $job_position[''] = 'Select job position...'; 
        return $job_position; 
  1. Create a Controller named Job_positions and add the following snippet. This controller fetches data from Job_positions_model and passes it to view. Remember you need to load the Job_positions_model, form helper, and Database; for me, I have loaded database and form helper in auto-load; also you can auto-load model. Read more about the Codeigniter loader class here.
< ? php 
if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 
class Job_positions extends CI_Controller { 
    function __construct() { 
        //load model 
        $this - > load - > model('Job_positions_model'); 
    function index() { 
        //fetch data from Job_positions_model 
        $data['job_positions'] = $this - > Job_positions_model - > get_job_positions(); 
        //pass data to view 
        $this - > load - > view('Job_positions_view', $data); 
  1. Next, create a view file named Job_positions_view and add the following snippet. Note: I will add bootstrap to make my input looks nice.
<!DOCTYPE html> 
<title>CI DB Dropdown input</title> 
<link rel="stylesheet" href=""> 
    <div class="col-sm-4"> 
        <h2>Our Dropdown input Demo</h2> 
        <div class="panel panel-default"> 
            <div class="panel-heading">The form</div> 
            <div class="panel-body"> 
                <!--dropdown input--> 
                <label class="title">Job position: </label> 
                <?php echo form_dropdown('job_position', $job_positions, '', 'class="form-control"');?> 
                    <button type="submit" class="btn btn-info">Submit</button> 
  1. Finally, save and preview your work through

My Demo is here: DEMO

I hope everything goes right; please provide your feedback in the comments below. Thank you for reading.

iexcel_post_nav(); ?>
0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments