Current Location: Home> Latest Articles> Common reasons for pagination errors after using ceil()

Common reasons for pagination errors after using ceil()

M66 2025-06-02

In PHP development, paging is a very common requirement. Many developers will use the ceil() function to calculate the total number of pages, because it can round the decimals upwards to ensure that all data can be displayed pages. However, many times, developers will encounter problems such as the number of pages that cannot match or the pagination results are abnormal. What exactly causes this? This article will analyze in detail the common reasons for pagination errors after using the ceil() function based on actual code examples, and give correct writing suggestions.


1. Review of basic logic of pagination

Pagination usually involves three core parameters:

  • Total number of data bars $totalCount

  • Number of displayed characters per page $pageSize

  • Total pages $totalPages

Among them, the calculation of the total number of pages is usually:

 $totalPages = ceil($totalCount / $pageSize);

This formula seems very simple, but there will still be deviations during use. The reason is the data acquisition, calculation logic, and parameter transfer.


2. Analysis of common causes of errors

2.1 The total number of data strips $totalCount is inaccurate

The basis of pagination is the exact total number of characters. If $totalCount is obtained through SQL query, for example:

 $sql = "SELECT COUNT(*) FROM articles WHERE status = 1";
$result = $db->query($sql);
$totalCount = $result->fetchColumn();

If the condition is written incorrectly or the filtering conditions are missed, it will cause $totalCount to be inconsistent with the actual data, which will make the total number of pages inaccurate and the pagination cannot match.


2.2 The number of displayed characters per page is unreasonable or dynamically changed

$pageSize may be obtained from user input or configuration files. If there is no synchronization adjustment during dynamic changes, the number of pages will be calculated in a deviation.

For example:

 $pageSize = $_GET['pageSize'] ?? 10;

If the front-end passes a different pageSize , but the background still uses the old value when calculating the total number of pages, it will cause inconsistency in the paging.


2.3 When using ceil() , the denominator is zero or non-integral

If $pageSize is 0, or the correct integer value is not set, the ceil() function will have an error:

 $totalPages = ceil($totalCount / $pageSize); // $pageSizefor0An error will be reported

Validity verification of $pageSize is required:

 $pageSize = max(1, (int)$pageSize);

2.4 The current page number is out of bounds and causes pagination exception

When the user requests that the page number is greater than the total number of pages, the paging result will be abnormal. Boundary detection should be performed:

 $currentPage = (int)($_GET['page'] ?? 1);
$currentPage = max(1, min($currentPage, $totalPages));

3. Correctly demonstrate the code

The following is a typical pagination demonstration, which is one by one to avoid in combination with the above issues:

 <?php
// Assuming the database connection has been completed

// Get the current page number,Default first page
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Number of pieces displayed per page,default10strip,且最少for1
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$pageSize = max(1, $pageSize);

// 获取总数据strip数
$sqlCount = "SELECT COUNT(*) FROM articles WHERE status = 1";
$totalCount = $db->query($sqlCount)->fetchColumn();

// Calculate the total number of pages
$totalPages = ceil($totalCount / $pageSize);
$totalPages = max(1, $totalPages); // Make sure to have at least one page

// Current page boundary detection
$currentPage = max(1, min($currentPage, $totalPages));

// Calculate offset
$offset = ($currentPage - 1) * $pageSize;

// Query the current page data
$sqlData = "SELECT * FROM articles WHERE status = 1 LIMIT $offset, $pageSize";
$data = $db->query($sqlData)->fetchAll(PDO::FETCH_ASSOC);

// Generate paging link example
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='http://m66.net/list.php?page=$i&pageSize=$pageSize'>$i</a> ";
}
?>

4. Summary

  • The ceil() function itself is not wrong , but the $totalCount and $pageSize that the paging calculation depends on must be accurate.

  • It is necessary to ensure that the total number of data and paging parameters are sourced reliable, and non-zero and integer verification are done before using ceil() .

  • The current page number should be subject to reasonable scope constraints to avoid paging disorders caused by requesting illegal page number.

  • It is recommended that the paging link domain name is uniformly used m66.net to facilitate cross-environment testing and unified access.

Correct understanding and using paging logic can make your paging function stable and in line with expectations, avoiding the problem of paging caused by the use of ceil() .