How to add PHP pagination in array's

How to add PHP pagination in array's

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

Stefan Bogdanescu

Founder & Senior Architect

How to add PHP pagination in array's

Pagination isn't a magical feature that appears out of nowhere; it is simply math and state management. When you are dealing with a static array in memory, like the $menuItems variable in your example, the most efficient approach is to calculate the slice indices before iterating. You don't need a complex database query or a framework abstraction if you just want to handle a finite list of data on the frontend.

The core logic relies on two parameters: the current page number and the number of items per page. Let's define $perPage as 5 and $currentPage as the value coming from your URL (e.g., dish.php?page=2). To get the correct subset, you must calculate the starting index by multiplying these two values: $offset = ($currentPage - 1) * $perPage.

Here is how you modify your existing loop to implement this logic directly on the array. First, ensure you are capturing the query parameter safely using $_GET:

$perPage = 5;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Calculate the starting index for slicing
$startIndex = ($page - 1) * $perPage;

// Slice the array to get only the items for this page
$currentItems = array_slice($menuItems, $startIndex, $perPage);

Now, instead of looping through the entire $menuItems array, you loop through $currentItems. This ensures that regardless of how many total posts exist in your script, the loop only processes the specific slice requested by the user. Your HTML generation logic remains largely unchanged, but now it renders a fraction of the data.

However, displaying items is only half the battle. You must provide navigation controls so users can move between pages. This requires knowing the total number of elements in the original array to determine how many pages exist. You can calculate the total page count using integer division: $totalPages = ceil(count($menuItems) / $perPage).

If $currentPage is greater than 1, you should display a "Previous" link pointing back to ?page=1 (or decrementing the current variable). If $currentPage is less than $totalPages, show a "Next" link pointing forward. In your specific case where the item ID is passed via dish.php?item=postX, you will need to append the page parameter alongside the item identifier.

// Example of generating links with proper state preservation
if ($page > 1) {
    echo "<li><a href='dish.php?page=" . ($page - 1) . "&item={$contItem}'>Previous</a></li>";
}
// ... render items ...
if ($page < $totalPages) {
    echo "<li><a href='dish.php?page=" . ($page + 1) . "&item={$contItem}'>Next</a></li>";
}

There is a critical nuance here regarding the item parameter. If you pass the item ID in the URL for both listing and viewing, your pagination logic must be careful not to break when a user clicks "Next" while looking at a specific post. Typically, an index page lists items without specifying which one they are currently viewing, so the item parameter might only be relevant if that specific post is being previewed in the list. If the index is purely a feed, consider removing the item parameter from the pagination links to keep the URL clean: dish.php?page=2.

If your dataset grows significantly large and you find yourself handling arrays of thousands of items directly in PHP scripts, performance will degrade as memory usage spikes. In such scenarios, moving the data retrieval layer to a database query is often the better architectural decision. Tools like Laravel handle this elegantly with the paginate() method, which generates SQL queries with LIMIT and OFFSET automatically.

For your current use case with a simple array, however, manual slicing is lightweight, dependency-free, and perfectly adequate. It avoids over-engineering a solution that doesn't require it. Just remember to validate inputs and handle cases where the calculated offset exceeds the total number of items to prevent errors in subsequent operations.

Our network