How to retrieve data from the database and display it in a html table using Codeigniter

How to retrieve data from the database and display it in a html table using Codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

Fixing CodeIgniter Data Retrieval and HTML Table Rendering

When attempting to fetch database records and display them in a standard HTML table, it is common to encounter structural errors in the view layer or logic leaks in the model. The code provided demonstrates a typical scenario where the data is successfully queried but fails to render correctly due to malformed HTML tags within the loop.

In the controller edit_content, the architecture follows the standard Model-View-Controller pattern of CodeIgniter 1.x. It loads the model editcontent_model, initializes necessary libraries like acl, and passes data to the view via $this->data. This approach is valid, provided that the variable name used in the view matches exactly what the controller assigns. Here, $this->data holds the result of $this->editcontent_model->get_contents().

The primary issue lies within the Model's get_contents() function. The method executes a query using $this->db->get('contents'), retrieves the results via $query->result(), and then attempts to load the view directly inside the model:

$this->load->view('edit_content/edit_content', $result);

While CodeIgniter allows models to return data, passing the result array directly to load->view is generally considered an anti-pattern. Models should focus solely on business logic and data retrieval. Passing data to views in the model bypasses the controller's responsibility for handling output formatting and can lead to tight coupling between layers. The standard practice is to have the model return a query object or an array (like $result), and let the controller pass that specific variable to the view using load->view('edit_content', $data).

The most critical error, however, is located in the View file. The HTML structure for the table rows is syntactically incorrect. Inside the foreach loop, there are two <tr> tags being opened consecutively:

<tr>
    <tr><?php echo $r['content']; ?></tr>
</tr>

This creates a nested table row structure, which renders poorly and may cause rendering issues in browsers. Additionally, the closing tag for the outer <tr> is placed outside the inner one incorrectly. To fix this, you need exactly one <tr> tag per iteration of the loop, containing the data cell (<td>) rather than a nested header row.

Here is the corrected view logic:

<table border="1" cellpadding="5" cellspacing="0">
    <thead>
        <tr>
            <th>Content</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($this->data as $row): ?>
            <tr>
                <td><?php echo htmlspecialchars($row['content']); ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

Several technical details warrant attention here. First, the loop variable name has been changed from $r to $row to align with common naming conventions, though this is purely stylistic. Second, and more importantly for security, htmlspecialchars() is used around the content output. In CodeIgniter 1.x, if you are not using the native query builder's escaping or a specific anti-XSS library, echoing raw database content directly can introduce Cross-Site Scripting (XSS) vulnerabilities. Wrapping the output ensures that any HTML tags or script attempts within the database content are rendered as text rather than executed code.

Finally, regarding the model implementation, removing the $this->load->view() call allows the controller to remain the sole entry point for rendering the response. The controller should look like this:

function edit_content() {
    parent::__construct();
    $this->load->model('editcontent_model');
    $this->load->helper('url');
    
    // Do not load ACL library unless explicitly required for this view
    // $this->load->library('acl'); 

    $data = $this->editcontent_model->get_contents();
    $this->load->view('edit_content/edit_content', $data);
}

By separating concerns—querying in the model, formatting HTML in the view, and coordinating flow in the controller—you ensure a maintainable application structure. If you are looking for more modern alternatives or detailed guides on MVC patterns in PHP frameworks, resources like https://laravelcompany.com offer extensive documentation that can help transition from older CI versions to newer ecosystems if your project requires it.

Our network