Displaying data from Database in CodeIgniter

Displaying data from Database in CodeIgniter

0 1711

In our previous blog, we store data in the student table. In this blog, we will retrieve the data from the database. Now, we will display the records in the student table.

Related Topics:

Connecting Database
Inserting Data to Database

Displaying data from Database in CodeIgniter:

Step 1 Go to the application/models directory. Open file Crud_model.php and update the file from the following code.


class Crud_model extends CI_Model
function insert_data($name,$roll_number,$class)
$query="insert into `student` values('','$name','$roll_number','$class')";

function display_data()
$query=$this->db->query("select * from `student`");
return $res;


Step 2 Open file application/controllers/Crud_controller.php and update the code.


defined('BASEPATH') OR exit('No direct script access allowed'); //for secuirty

class Crud_controller extends CI_Controller

//use constructor to connect with database and load model
public function __construct()
$this->load->database(); ///load database
$this->load->model('Crud_model'); ///load model

//function to insert data from database
public function insert_fun()

//load view

// after submission code



echo "Data inserted Successfully";

// function to display data from database
public function display_fun()

Step 3 Open folder application/views and create a new file display_view.php.


<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<div class="container">
<h2>Student Data</h2>
<table class="table table-striped" style="width:50%">
<th>Roll Number</th>
<?php foreach($student as $row){?>
<td><?php echo $row["name"];?></td>
<td><?php echo $row["roll_number"];?></td>
<td><?php echo $row["class"];?></td>
<?php } ?>


Step 4 Now, enter the given URL into the browser to see the data.


Related Topic:

Codeigniter Interview Questions


Best WordPress Hosting


Leave a Reply

    Waiting for your comments