display data from database to dropdown CodeIgniter

display data from database to dropdown CodeIgniter

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Populating Dropdowns in CodeIgniter: Fixing Data Rendering Issues

You are encountering a classic issue where data is being retrieved from the database but rendered outside the intended HTML structure. In your current implementation, the dropdown list remains empty while the raw text appears at the top of the page because the logic for fetching data and rendering HTML options is fragmented across multiple files and methods.

The core problem lies in how you are handling the return value of your model method versus how you are iterating over it in the view.

Data Flow Architecture in CodeIgniter

In a standard MVC architecture like CodeIgniter, the controller acts as the intermediary between the database model and the presentation layer (the view). The model should strictly handle data retrieval, while the view handles the HTML generation.

Looking at your Model.php, the method getAllGroups() executes a query but immediately echoes the results to the output buffer:

$query = $this->db->query('SELECT description FROM location');

foreach ($query->result() as $row) {
    echo $row->description;
}

When you call this function in your controller within index(), the string of descriptions is printed directly to the browser output before any view is loaded. This explains why the text appears at the top of your page:

$data['groups'] = $this->delivery_model->getAllGroups();

By the time you assign this result to a variable and pass it to the view, the data has already been "consumed" by echo. Furthermore, your controller does not return a standard array or object that the view can iterate over; it returns whatever was left in the output buffer, which is essentially nothing useful for a dropdown.

Correcting the Model Method

To fix this, your model method must return data rather than displaying it. The most flexible approach is to return an array of associative arrays derived from the database result object. This allows your view to iterate over the data and construct HTML elements dynamically.

Here is the corrected implementation for getAllGroups():

public function getAllGroups() {
    $query = $this->db->get('location'); // Using get() is cleaner than raw query() for single tables
    
    $data = [];
    
    foreach ($query->result() as $row) {
        $data[] = [
            'id' => $row->id,       // Always include a unique identifier if available
            'description' => $row->description
        ];
    }
    
    return $data;
}

Notice that we initialize an empty array $data, populate it with the necessary fields, and then return the array. This keeps the data encapsulated within the application logic rather than leaking into the browser output.

Updating the Controller

In your Delivery_controller, you are currently calling the model method inside the view file, which is a violation of separation of concerns. Additionally, you are storing the result in $data['groups'] but never actually passing that specific variable to the view's data array before rendering it.

Update your controller as follows:

public function index() {
    $this->load->model('delivery_model');

    // Fetch data from model BEFORE loading views
    $group_options = $this->delivery_model->getAllGroups();

    $data['title'] = 'Warehouse - Delivery';
    $data['groups'] = $group_options; // Pass the fetched array to the view

    $this->load->view('include/header', $data);
    $this->load->view('include/navbar', $data);
    $this->load->view('delivery_view', $data);
    $this->load->view('include/sidebar', $data);
    $this->load->view('include/footer', $data);
}

Rendering the Dropdown in the View

Finally, you need to update your View.php. The original code attempted to call the model directly inside the view and used incorrect syntax for iterating over arrays. You should iterate over the $groups array passed from the controller.

Here is the corrected HTML structure:

<select class="form-control">
    <?php foreach ($groups as $group): ?>
        <option value="<?php echo htmlspecialchars($group['description']); ?>">
            <?php echo htmlspecialchars($group['description']); ?>
        </option>
    <?php endforeach; ?>
</select>

Key adjustments here include:

  1. Iterating directly over $groups instead of calling the model again.
  2. Using htmlspecialchars() to prevent Cross-Site Scripting (XSS) vulnerabilities by escaping user-generated content from the database before rendering it in HTML.
  3. Ensuring the syntax matches PHP's standard array iteration pattern rather than trying to construct strings inside a loop incorrectly.

Alternative Approach: Passing Data via load->model

If you prefer not to load the model in the controller, you can use the load->model() method with data passing capabilities (available in CodeIgniter 3 and higher versions). However, explicitly fetching data in the controller and passing it to the view remains the most explicit and readable pattern for beginners.

By separating data retrieval from rendering, you ensure that your dropdowns populate correctly without leaking raw text into your page header. This structure adheres to best practices and makes debugging significantly easier if the database changes later.

Our network