Displaying data from Database in CodeIgniter
×

Displaying data from Database in CodeIgniter

2644

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.

Crud_model.php

<?php
class Crud_model extends CI_Model
{
function insert_data($name,$roll_number,$class)
{
$query="insert into `student` values('','$name','$roll_number','$class')";
$this->db->query($query);
}

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

}

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

Crud_controller.php

<?php
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()
{
parent::__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
$this->load->view('insert_view');

// after submission code
if($this->input->post('insert'))
{

$name=$this->input->post('name');
$roll_number=$this->input->post('roll_number');
$class=$this->input->post('class');

$this->Crud_model->insert_data($name,$roll_number,$class);

echo "Data inserted Successfully";
}
}

// function to display data from database
public function display_fun()
{
$data['student']=$this->Crud_model->display_data();
$this->load->view('display_view',$data);
}
}
?>

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

display_view.php

<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>

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

</body>
</html>

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

http://localhost/ci/index.php/Crud_controller/display_fun

Related Topic:

Codeigniter Interview Questions

Output:



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments