How to Ajax Pagination with Column Sorting in PHP

How to Ajax Pagination with Column Sorting in PHP

How to Ajax Pagination with Column Sorting in PHP

0 Sales


Ajax Pagination enhances the user experience when viewing paginated material on a website page. Using jQuery and Ajax, the navigation file is recorded from the server without requiring a page refresh. This article will show you how to use the column sorting feature in PHP to improve the Ajax pagination capability.

The majority of the time, the records are extracted from the data and displayed in an HTML table. Along with pagination, the row filtering function is highly beneficial for improving the user experience of the content list table. The header rows of the HTML table will be editable, allowing the user to sort the entries in lowest to highest. Ajax pagination with pillar sort enables users to easily access a huge data list via pagination links and sort the data list by selecting columns in lowest to highest.


The following functionality will be implemented in the Ajax Pagination using Column Arranging module.


- Using PHP, get current data from the Database server and list them in an HTML table.

- By using Ajax Pagination module, add pagination hyperlinks to the information list table.

- Make the header columns of an HTML table accessible for information filtering.

- Enables the customers to paginate and arrange data without having to reload the page.


Take a look at the file structure before beginning to construct Ajax pagination with sorting by column in PHP.


├── dbConfig.php
├── index.php
├── getData.php
├── Pagination.class.php
├── js/
│   └── jquery.min.js
└── css/
    ├── bootstrap.min.css
    └── style.css



Create Database Table


To hold dynamic information in the database, a table is necessary. In the MySQL database, the following SQL creates a people database with some fundamental columns.

CREATE TABLE `users` (
  `first_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `country` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1=Active | 0=Inactive',
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


Pagination Library


This customized PHP module aids in the development of pagination links through Ajax. To combine ajax pagination using column filtering in PHP, use the following configuration parameters.


- totalRows – The total number of rows.

- perPage — Keep track of the numbers that will be displayed on each page.

- currentPage – The page number that is currently in use.

- contentDiv – The ID of the HTML element in which the Ajax response information will be displayed.

- link func – The name of the function that performs the sort by column name function.


 * CodexWorld is a programming blog. Our mission is to provide the best online resources on programming and web development. 
 * This Pagination class helps to integrate ajax pagination in PHP. 
 * @class        Pagination 
 * @author        CodexWorld 
 * @link        http://www.codexworld.com 
 * @contact        http://www.codexworld.com/contact-us 
 * @version        1.0 
class Pagination{ 
    var $baseURL        ''; 
    var $totalRows      ''; 
    var $perPage        10; 
    var $numLinks       =  3; 
    var $currentPage    =  0; 
    var $firstLink      '‹ First'; 
    var $nextLink       '>'; 
    var $prevLink       '<'; 
    var $lastLink       'Last ›'; 
    var $fullTagOpen    '

No records found...';             }             ?> tbody> table> echo $pagination->createLinks(); ?> div> div>


Sort and Get Pagination Data (getData.php)


The Ajax request (called by the columnSorting() method) loads the getData.php code to obtain the paginated entries from the database.

- Retrieves data based on sort order, paging limit, and offset.
- Generate a data list that is sorted by column attribute value and includes pagination links.
- The HTML representation of the production database with column sorting is returned.


    // Include pagination library file 
    include_once 'Pagination.class.php'; 
    // Include database configuration file 
    require_once 'dbConfig.php'; 
    // Set some useful configuration 
    $offset = !empty($_POST['page'])?$_POST['page']:0; 
    $limit 5; 
    // Set conditions for column sorting 
    $sortSQL ''; 
    if(!empty($_POST['coltype']) && !empty($_POST['colorder'])){ 
        $coltype $_POST['coltype']; 
        $colorder $_POST['colorder']; 
        $sortSQL " ORDER BY $coltype $colorder"; 
    // Count of all records 
    $query   $db->query("SELECT COUNT(*) as rowNum FROM users"); 
    $result  $query->fetch_assoc(); 
    // Initialize pagination class 
    $pagConfig = array( 
        'totalRows' => $rowCount, 
        'perPage' => $limit, 
        'currentPage' => $offset, 
        'contentDiv' => 'dataContainer', 
        'link_func' => 'columnSorting' 
    $pagination =  new Pagination($pagConfig); 
    // Fetch records based on the offset and limit 
    $query $db->query("SELECT * FROM users $sortSQL LIMIT $offset,$limit"); 
                           #ID              First Name              Last Name              Email              Country              Status                            if($query->num_rows 0){             while($row $query->fetch_assoc()){         ?>                               echo $row["id"]; ?>                 echo $row["first_name"]; ?>                 echo $row["last_name"]; ?>                 echo $row["email"]; ?>                 echo $row["country"]; ?>                 echo ($row["status"] == 1)?'Active':'Inactive'?>                      }         }else{             echo ' No records found...';         }         ?>                         echo $pagination->createLinks(); ?> } ?>




Column sorting is the finest and most user-friendly technique to sort list by columns if you want to add sort capabilities to the data list table. This sample script demonstrates how to add ajax pagination functionality with table sorting to a PHP table. By applying the sort function to the HTML table's header columns, the column title becomes accessible, and the consumer may change the data source to sort in ascending or descending order.


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : Jan 18, 2022

Updated Date : Jan 23, 2022


Comments : 0

Downloads : 0