Current Location: Home> Latest Articles> How to Implement Website Pagination and Sorting with PHP and CGI

How to Implement Website Pagination and Sorting with PHP and CGI

gitbox 2025-07-27

Introduction

Website pagination and sorting features are crucial for improving user experience and browsing efficiency. In this article, we will discuss how to implement these two features using PHP and CGI technologies.

Pagination Feature

Implementation Principle

The principle behind pagination is to split the data from the database into multiple pages for display, allowing users to browse different pages of data by clicking on page numbers. First, we need to get the total amount of data from the database and calculate the total number of pages. Next, based on the selected page number, we retrieve and display the corresponding data from the database.

Code Example

Here is an example of PHP code to implement pagination:

// Get total data count and number of items per page
$sql = "SELECT COUNT(*) as total FROM table_name";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($result);
$total = $row['total'];
$per_page = 10;
// Calculate total pages
$total_pages = ceil($total / $per_page);
// Get the current page from user input
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Calculate start and end data rows for the current page
$start = ($current_page - 1) * $per_page;
$end = $start + $per_page;
// Get data for the current page
$sql = "SELECT * FROM table_name LIMIT $start, $per_page";
$result = mysqli_query($connection, $sql);
// Display data
while ($row = mysqli_fetch_assoc($result)) {
    // Display data content
}
// Output pagination navigation
for ($i = 1; $i <= $total_pages; $i++) {
    // Output page number navigation
}

Sorting Feature

Implementation Principle

The sorting feature allows users to sort data in ascending or descending order based on different fields, making it easier for users to find and browse data. To implement sorting, we need to construct an SQL query based on user input, which can be retrieved via URL parameters or form submissions to get the sorting field and sorting type (ascending or descending).

Code Example

Here is an example of CGI code to implement sorting:

// Get user-selected sorting field and type
$sort_field = isset($_GET['sort_field']) ? $_GET['sort_field'] : 'default_field';
$sort_type = isset($_GET['sort_type']) ? $_GET['sort_type'] : 'asc';
// Build SQL query
$sql = "SELECT * FROM table_name ORDER BY $sort_field $sort_type";
$result = mysqli_query($connection, $sql);
// Display data
while ($row = mysqli_fetch_assoc($result)) {
    // Display data content
}
// Output sorting links
echo "Sort Field 1 Ascending";
echo "Sort Field 1 Descending";
echo "Sort Field 2 Ascending";
echo "Sort Field 2 Descending";

Conclusion

By using PHP and CGI, we can easily implement website pagination and sorting features. Pagination improves the efficiency of browsing data, while sorting allows users to organize data based on their preferences. During implementation, developers should take care to prevent SQL injection and other security issues. Furthermore, additional features like filtering and searching can be added to further enhance the user experience.