CodeIgniter Display Data

In this section, We'll understand how to display data from database. We'll use tbl_users table to display records.

Using a text editor, copy the below code in your controller called User.php. In it, place this code and save it to your application/controllers/ directory:

<?php
  
   class User extends CI_Controller {
   
      public function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('form', 'url')); 
      } 
	
      public function index() {
			
         /* Load form validation library */ 
         $this->load->library('form_validation');
			
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
			
         if ($this->form_validation->run() == FALSE) { 
            $this->load->view('user_form'); 
         } 
         else { 
                    $this->load->model('user_model');
		    $this->user_model->save();
		    $success = "Submit successfully!";
                    $this->load->view('user_form', compact('success')); 
         } 
      }
	  
	  public function getdata()
	     {
		   $this->load->model('user_model');
		   $data['record'] = $this->user_model->getdata();
		   $this->load->view('display_users', $data);
	     }
  }
?>

Using a text editor, create a new view file called display_users.php. In it, place this code and save it to your application/views/ directory:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
	<table width="200" class="table">
	  <tr>
		<th>Sr No</th>
		<th>Name</th>
		<th>Email</th>
		<th>Mobile</th>
		<th>Action</th>
	  </tr>
	  <?php
	     $i = 1;
		 foreach ($record as $result)
		  {
			 echo "<tr>";
			 echo "<td>".$i."</td>";
			 echo "<td>".$result->name."</td>";
			 echo "<td>".$result->email."</td>";
			 echo "<td>".$result->contact_no."</td>";
			 echo "<td><a href='".site_url()."/user/getrecordbyid/".$result->id."'>Edit</a>  <a href='".site_url()."/user/deleteuserbyid/".$result->id."'>Delete</a></td>";
			 echo "</tr>";
		  }
	  ?>
	</table>
  </div>
 </body>
</html>

Using a text editor, copy the below code in your model called User_model.php. In it, place this code and save it to your application/models/ directory:

<?php
class User_model extends CI_Model
{
    public function save()
    {
	   $data['name'] = $this->input->post('name');
	   $data['email'] = $this->input->post('email');
	   $data['contact_no'] = $this->input->post('contact_no');
	   $this->db->insert('tbl_users', $data);
	   }
	   
	   public function getdata()
	     {
		    return $this->db->get('tbl_users')->result();	
	     }
  }

To try your view, visit your site using a URL similar to this one:

http://localhost/codeigniter/index.php/user/getdata

codeigniter display data from database