How to display data codeigniter

Create a view file display.php and save the below code it in application/views/display.php.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Display Data</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">         
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
        <?php
            foreach($data as $d){
        ?>
      <tr>
        <td><?php echo $d->id; ?></td>
        <td><?php echo $d->name; ?></td>
        <td><?php echo $d->email; ?></td>
      </tr>
        <?php
            }
        ?>
    </tbody>
  </table>
</div>

</body>
</html>

Create a controller file Demo.php and save it in application/controller/Demo.php.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Demo extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model("Demo_Model");
    }
    function insert(){
        $this->load->view("insert");
    }
    
    function save(){
        $data = $this->input->post();
        $result = $this->Demo_Model->save("demo",$data);
        if (!empty($result)){
            redirect(base_url("demo/insert"));
        }
    }
    
    function display(){
        $data["data"] = $this->Demo_Model->get_all("demo");
        $this->load->view("display",$data);
    }
}
    

Create a model file Demo.php and save it in application/models/Demo_Model.php.

<?php

class Demo_Model extends CI_Model{
    
    function save($table_name,$data){
        $this->db->insert($table_name,$data);
        return $this->db->insert_id();
    }
    
    function get_all($table_name){
        $this->db->select("*");
        $this->db->from($table_name);
        $query = $this->db->get();
        return $query->result();
    }
}

Let us execute this example by the following URL in the browser. This URL may be different based on your website.

https://howtowebcode.com/demo/display