Limit pagination page number

Limit pagination page number

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Limiting Pagination Controls for Large Datasets

When implementing pagination on a search results page, the default behavior often feels unwieldy once the dataset exceeds a few thousand rows. The provided PHP snippet demonstrates a common pattern using the deprecated mysql_* functions to calculate total pages and render navigation links. While the logic for determining $Num_Pages is mathematically correct—handling edge cases where row counts are divisible by page size or when there are fewer rows than the limit—the output becomes problematic at scale. If you have 5,000 records with a page size of 10, your pagination control renders links from 1 to 500. Clicking on page 400 is likely useless if the search filters only match 5 items, and users get lost in a sea of numbers they cannot scroll through.

The core challenge is not just hiding the current page number, but intelligically truncating the visible range while maintaining navigation to the "far" ends of the dataset. You need to define a maximum number of clickable page links (e.g., 10) and calculate which pages fall within that window relative to the current $Page.

To achieve this without rewriting your entire pagination engine, you must add logic before the loop that renders the links. First, establish a constant or variable for the maximum visible links, perhaps named $Max_Pages_Show set to 10. Then, calculate the start and end of your visible range based on the current page.

$Max_Pages_Show = 10; // Total visible page numbers

// Calculate the starting point of the visible window
$Start_Page = max(1, $Page - floor($Max_Pages_Show / 2));

// Calculate the ending point, ensuring we don't exceed total pages
$End_Page = min($Num_Pages, $Page + ceil(($Max_Pages_Show - 1) / 2));

This snippet ensures that if you are on page 150 with 500 total pages, your visible range might start at 146 and end at 153. If you are near the beginning (e.g., page 5), the max(1, ...) function prevents the window from extending into negative indices or zero. Similarly, the min($Num_Pages, ...) guard ensures that when approaching the last page, the loop doesn't try to display non-existent future pages.

You then modify your existing loop to check if the current iterator $i falls within this calculated range:

for ($i = 1; $i <= $Num_Pages; $i++) {
    // Only render links for pages within our calculated window
    if ($i >= $Start_Page && $i <= $End_Page) {
        if ($i != $Page) {
            echo " <a href ='...&Page=$i'>{$i}</a> ";
        } else {
            echo "<b>{$i}</b>"; 
        }
    } else {
        // Optional: Render ellipsis (...) for gaps between current range and ends
        if ($i == $Start_Page - 1 || $i == $End_Page + 1) {
             echo " ... ";
        }
    }
}

There is a significant caveat here regarding the ... ellipses. Rendering them adds visual context but increases complexity. If you strictly only want to limit the numbers and hide everything else, simply removing the else block suffices. However, omitting the ellipsis can be confusing; users might think pages 140 through 155 don't exist if they see a jump from 146 directly to 160.

Beyond the PHP logic, it is worth noting that the underlying query and connection methods in your source code rely on mysql_* functions, which were deprecated in PHP 5.5 and removed entirely in PHP 7.0. Relying on these for pagination calculations is a technical debt risk. If you are maintaining legacy code, this truncation logic solves the immediate UI issue. If you have the opportunity to refactor, consider migrating to PDO or MySQLi. Modern frameworks like Laravel provide robust query scopes that handle LIMIT and pagination offsets more safely, preventing SQL injection vulnerabilities inherent in concatenating $Page_Start directly into the string without parameter binding.

Furthermore, consider whether truncating the page numbers is the right UX decision for your specific search tool. If users are searching for a very specific record in a massive table, seeing "..." might obscure their ability to jump directly to a specific segment of data. In such cases, offering a text input box for direct page entry alongside the truncated numeric links often provides the best balance between simplicity and power.

Our network