I am using CodeIgniter and displaying data using AJAX in the table using jquery datatable.
https://datatables.net/examples/data_sources/server_side.html
What I am doing is, After login
/*This is the small code after getting the right result */
if ($result) {
$this->load->view('list_details');
} else {
$this->session->set_flashdata('invalid_password', 'Invalid Username And Password');
$this->load->view('login');
}
Now in the list details page, I am displaying the total number of the record using AJAX in datatable .
list_detials (view)
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>assets/css/jquery.dataTables.min.css">
</head>
<body>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Sr.no</th>
<th>First name</th>
<th>middlename</th>
<th>Last name</th>
<th>Disease </th>
<th>Thermal</th>
<th>Last fallowup</th>
<th>Progress </th>
</tr>
</thead>
<tbody></tbody>
</table>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/additional-methods.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/validation.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "<?php base_url('Clinic_control/list_data_ajax')?>",
"type": "POST"
},
"columns": [
{ "data": "id" },
{ "data": "firstname" },
{ "data": "middlename" },
{ "data": "lastname" },
{ "data": "disease" },
{ "data": "thermal" },
{ "data": "last_fallowup" },
{ "data": "progress" }
]
});
});
</script>
</body>
</html>
Controller for AJAX
function list_data_ajax() {
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$books =$this->Clinic_model->get_list();
$data['draw'] = 1;
$data['recordsTotal'] = count($books);
$data['recordsFiltered'] = count($books);
foreach ($books as $key => $row) {
$arr_result[] = array(
"id" =>$row->id,
"firstname" => $row->firstname,
"middlename" => $row->middlename,
"lastname" => $row->lastname,
"disease" => $row->disease,
"thermal" => $row->thermal,
"last_fallowup" => $row->last_fallowup,
"progress" => $row->progress
);
}
echo json_encode($arr_result);
}
Model
public function get_list(){
$query=$this->db->get('test_record');
$result=$query->result();
if ($result) {
return $result;
} else {
return 0;
}
}
arr_result
array should get merged withdata
array... you may take a closer look here stackoverflow.com/questions/47885399/… - sintakonte