Working with jQuery DataTables server-side processing using PHP and MySQL

Working with jQuery DataTables server-side processing using PHP and MySQL

In this post we will give you information about Working with jQuery DataTables server-side processing using PHP and MySQL. Hear we will give you detail about Working with jQuery DataTables server-side processing using PHP and MySQLAnd how to use it also give you demo for it if it is necessary.

Working with jQuery DataTables server-side processing using PHP and MySQL

In this PHP Tutorial, I am going to tell you how to use jQuery datatable plugin with Server-side processing with PHP and MySQL.

jQuery DataTables have lots of flexibility built-in by default. For example it provides sorting, pagination, searching functionality that is not so easy in HTML tables.

Using jQuery datatable, you transform your HTML table from classical to DataGridView component.

In this example, I will show you basic example of jQuery datatable to list whole data at once from MySQL database.

If there are large amount of records in your database then it can be a performance issue to get all data from server at once.

In next example, I will show you how to integrate server side pagination, searching and sorting using jQuery DataTables in PHP MySQL.

For this example, I need to obtain the library of jQuery and jQuery DataTables.


Step1: Create Employee Table

To start with this example, You should have a table with some records so I will create a new table “employee” in MySQL database by running following query :

CREATETABLE'employee' (
 'id'int(11) NOTNULLAUTO_INCREMENT,
 'name'varchar(255) NOTNULL,
 'age'varchar(20) NOTNULL,
 'salary'varchar(100) NOTNULL,
 'created_at'timestampNOTNULLDEFAULTCURRENT_TIMESTAMP,
 'updated_at'datetimeDEFAULTNULL,
 PRIMARYKEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=12DEFAULTCHARSET=latin1



Step2: Create employee.html file

In this step, I will create “employee.html” file and include the DataTables CSS in the top and in the bottom include JS files and instantiate the DataTable on the table.


employee.html

<!DOCTYPE html>
<html>
<head>
   <title>Working with jQuery DataTables server-side processing using PHP and MySQL</title>
   <linkrel="stylesheet"type="text/css"href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
  
</head>
<body>

<divclass="container">
  <h2>Working with jQuery DataTables server-side processing using PHP and MySQL</h2>
  <tableid="dataTable-example">
    <thead>
      <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Age</th>
          <th>Salary</th>

      </tr>
    </thead>
  </table>
</div>

</body>
<script type="text/javascript"src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript"charset="utf8"src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
      $('#dataTable-example').dataTable({
        "bProcessing":true,
        "sAjaxSource":"employee.php",
        "aoColumns": [
              { mData:'id' } ,
              { mData:'name' },
              { mData:'age' },
              { mData:'salary' }

            ]
      });  
  });
</script>
</html>


Step 3: Create employee.php File

In this step, I will create a “employee.php” file and configure the database and write a simple MySQL select query to get records from employee table.

<?php

define (DB_USER, "root");
define (DB_PASSWORD, "demo");
define (DB_DATABASE, "demo");
define (DB_HOST, "localhost");

$conn=new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


$query="SELECT * FROM employee";
$result=$conn->query($query);

$data=array();
while($row=$result->fetch_array(MYSQLI_ASSOC)){
  $data[] =$row;
}

$results= ["sEcho"=>1,
          "iTotalRecords"=>count($data),
          "iTotalDisplayRecords"=>count($data),
          "aaData"=>$data ];

echojson_encode($results);

?>

DataTables expect some attributes in the returned json data and they are :

  • iTotalRecords – It should be the total number of records before applying the filters.
  • iTotalDisplayRecords – It should be the total number of records after applying the filters.
  • sEcho – Must be integer type for security reason. This is an unaltered copy of sEcho sent from the client side.
  • aaData – It will return the array of data from the server.


jQuery DataTable server side sorting, pagination and searching using PHP and MySQL

Hope this code and post will helped you for implement Working with jQuery DataTables server-side processing using PHP and MySQL. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  70  =  76

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US