Server-side Processing using PHP with MySQL Example

79

 

DataTables is a jQuery library used to display the list of records in a HTML table with an intuitive interface. It includes features like search, pagination, sort, filter and more. In a previous article, we have seen a custom code for search and pagination for the list of records. Using DataTables we can limit the number of records to be displayed per page.

DataTables extension provides both client-side and server-side processing. In this article, we are going to show the database results by using DataTables server-side processing. A PHP file calls the domain class function by sending the database table, column and the configuration details. The domain class executes the query to read requested columns and returns array of results. This resultant array is encoded to the JSON format which will send the response of the DataTables AJAX script.

DataTable HTML Code

The following code shows the HTML code for the DataTable. This code contains required CSS and JavaScript library includes on the Head portion. I have added styles to customize the DataTable UI by overriding the library styles. The HTML table tag has an id attribute which is used to initialize the table as a DataTable.

DataTable Initialization using jQuery

The following code shows the jQuery DataTable initialization by setting the required DataTable property. In this code, the serverSide property is set as true and the server side file path is specified for the ajax property. These two are used to enable the server side processing for the DataTable.

Server-Side Processing

The following PHP code is adapted from the example given in the DataTable official website. It declares the database configuration details, table name, columns and sends then to the domain class to process the SELECT query.

 

Leave A Reply

Your email address will not be published.