DataTables Example – Server-Side Processing With PHP

https://www.phpflow.com/php/datatables-example-server-side-processing-with-php/

DataTables Example – Server-Side Processing With PHP

Last Updated On: October 22, 2017arrow-up-right| By: Parvezarrow-up-right

In previous post Data Table jQuery Pluginarrow-up-right, we have learn what is jQuery datatable plugin and how to use jQuery datatable in your application,now in this tutorial i will describe how to use data table with server side scripting.I am using PHP and MySQL to get records from server side.In this tutorial our aim to get data from MySQL with help of php and passed data to jQuery datatable constructor.

Also Checkout other tutorial of Datatable,

datatable-with-php

There Are Following Steps Need To Achieve Over Goal:

Step 1: we will include jquery datatable and jquery library.

12345678

<!-- DataTables CSS --><link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"><!-- jQuery --><script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script><!-- DataTables --><script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

Step 2: Created HTML table in your web page.

123456789101112131415161718192021

<div class=""> <table id="example" class="display" width="100%" cellspacing="0"> <thead> <tr> <th>Empid</th> <th>Name</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Empid</th> <th>Name</th> <th>Salary</th> </tr> </tfoot> </table> </div>

Step 3: Apply Datatable functionality on our HTML table.

1234567891011

$( document ).ready(function() {$('#example').dataTable({ "bProcessing": true, "sAjaxSource": "response.php", "aoColumns": [ { mData: 'Empid' } , { mData: 'Name' }, { mData: 'Salary' } ] }); });

Step 4: Now we will create response.php file and write down below code.

1234567891011121314151617

$data = array( array('Name'=>'parvez', 'Empid'=>11, 'Salary'=>101), array('Name'=>'alam', 'Empid'=>1, 'Salary'=>102), array('Name'=>'phpflow', 'Empid'=>21, 'Salary'=>103) ); $results = array( "sEcho" => 1, "iTotalRecords" => count($data), "iTotalDisplayRecords" => count($data), "aaData"=>$data);/*while($row = $result->fetch_array(MYSQLI_ASSOC)){ $results["data"][] = $row ;}*/ echo json_encode($results);

Here we have taken constant array instead of MySQL records, I am assuming you will replace this constant array with your MySQL result set.

Live Demoarrow-up-right

Download Source Codearrow-up-right

Phparrow-up-right datatable with ajaxarrow-up-right, datatable with phparrow-up-right, DataTables Examplearrow-up-right, Server-side Processing with PHParrow-up-right. permalinkarrow-up-right.

Last updated