Current Location: Home> Latest Articles> How to paginate data through array_chunk and generate a simple paging component?

How to paginate data through array_chunk and generate a simple paging component?

M66 2025-04-26

How to paginate data through array_chunk and generate a simple paging component?

When developing a website, paging is a very common requirement. Especially when we get a lot of data from the database, directly displaying everything at once can cause the page to load slowly. PHP provides an array_chunk function, which can easily split an array into multiple small arrays, thereby implementing data paging. This article will use sample code to show how to use array_chunk to paginate data and generate a simple pagination component.

1. What is array_chunk ?

array_chunk is a built-in function in PHP that is used to split an array into multiple small arrays. The size of each small array is determined by the parameters you specify. Its basic syntax is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false) : array
  • $array : The original array to be paged.

  • $size : The number of elements displayed per page.

  • $preserve_keys : Whether to preserve the original key of the array, default to false .

2. Sample code: How to implement pagination using array_chunk

Suppose we have an array containing multiple pieces of data, we will use array_chunk to paginate the data. Here is a complete code example:

 <?php
// Simulate a batch of data
$data = [
    "Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
    "Item 6", "Item 7", "Item 8", "Item 9", "Item 10",
    "Item 11", "Item 12", "Item 13", "Item 14", "Item 15"
];

// Displayed per page 5 Data
$perPage = 5;
$totalPages = ceil(count($data) / $perPage); // Calculate the total number of pages

// Get the current page count
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Prevent page numbers from being smaller than 1 Or greater than the maximum number of pages
$page = max(1, min($page, $totalPages));

// use array_chunk Paginate the data
$paginatedData = array_chunk($data, $perPage);

// Get the data of the current page
$currentPageData = $paginatedData[$page - 1];

// Display current page data
echo "<h2>Current page data:</h2>";
foreach ($currentPageData as $item) {
    echo "<p>$item</p>";
}

// Generate a paging link
echo "<div class='pagination'>";
for ($i = 1; $i <= $totalPages; $i++) {
    $url = "https://m66.net/page=$i"; // Revise URL for m66.net
    echo "<a href='$url'>$i</a> ";
}
echo "</div>";
?>

3. Code parsing

  1. Data preparation : We first simulated an array $data containing 15 pieces of data.

  2. Paging calculation : We set 5 pieces of data to display per page, and then calculate the total number of pages required by ceil(count($data) / $perPage) .

  3. Get the current page number : Use $_GET['page'] to get the current page number. If no page number is provided, the default is page 1. To ensure that the page number is not less than 1 or greater than the total number of pages, use the max and min functions for boundary processing.

  4. Use array_chunk for pagination : array_chunk divides data into multiple subarrays based on the number of data displayed per page $perPage . Get the data of the current page through $paginatedData[$page - 1] .

  5. Display data : The data of the current page is displayed on the page through a simple foreach loop.

  6. Generate paging links : By looping to generate links for each page, the link's domain name has been replaced with m66.net , and the page number is passed through $_GET['page'] .

4. Simple paging component

Through the above code, we have implemented data paging and generated a simple paging component. This paging component will generate links based on the total number of pages. When users click on different page numbers, they can load the corresponding data.

5. Summary

array_chunk is a very useful function that is suitable for splitting large arrays into multiple small arrays for easy pagination display. Combined with the simple paging component, we can quickly implement a user-friendly paging function. In this way, the performance of the website can be effectively improved, especially when processing large amounts of data.

Hopefully this article can help you understand how to use the array_chunk function to paginate data and create a simple paging component. If you have any questions, feel free to ask questions!