Current Location: Home> Latest Articles> Practical case: Use end() to realize the preview of the last page of the pagination system

Practical case: Use end() to realize the preview of the last page of the pagination system

M66 2025-06-02

When developing a paging system, we usually encounter the need to preview the data on the last page. To better implement this function, PHP provides many powerful array manipulation functions, where the end() function is especially useful when implementing the last page preview in a paging system. This article will analyze in detail how to use the end() function to implement this function through practical cases.

What is the end() function?

The end() function in PHP is a very practical array operation function. It points the internal pointer of the array to the last element of the array and returns the value of that element. If the array is empty, the end() function returns FALSE .

How to use end() to implement the last page preview in the pagination system?

In a pagination system, we often need to display the last page of the data. To achieve this, we can extract the last page data in the array through array operations. Using the end() function can easily get the last element of the array.

The following is a simplified practical case that demonstrates how to obtain and preview the last page in the paged data through the end() function.

Practical case: PHP pagination system realizes the last page preview

1. Create data

Suppose we have a set of data that is fetched from the database. To simplify the demonstration, assume that this data is already stored in an array, each element representing a data record.

 <?php
$data = [
    ['id' => 1, 'name' => 'Item 1'],
    ['id' => 2, 'name' => 'Item 2'],
    ['id' => 3, 'name' => 'Item 3'],
    ['id' => 4, 'name' => 'Item 4'],
    ['id' => 5, 'name' => 'Item 5'],
    ['id' => 6, 'name' => 'Item 6'],
    ['id' => 7, 'name' => 'Item 7'],
    ['id' => 8, 'name' => 'Item 8'],
    ['id' => 9, 'name' => 'Item 9'],
    ['id' => 10, 'name' => 'Item 10']
];
?>

2. Pagination parameters

In order to implement paging, we need to know how many pieces of data are displayed per page. For example, 3 pieces of data are displayed per page.

 <?php
$perPage = 3;
$totalItems = count($data);
$totalPages = ceil($totalItems / $perPage);
$currentPage = 4;  // Assume that the current request is 4 Page
?>

3. Get the last page of data

Through the end() function, we can get the data of the last item in the array. If we want to get the last page data in the paging system, we just need to calculate the starting position of the current page and extract the corresponding record.

 <?php
// 计算当前Page的起始索引
$startIndex = ($currentPage - 1) * $perPage;
$endIndex = min($startIndex + $perPage, $totalItems);

// 获取当前Page数据
$currentPageData = array_slice($data, $startIndex, $endIndex - $startIndex);

// 如果当前Page是最后一Page,Available end() 获取最后一Page的最后一条记录
$lastItemOnCurrentPage = end($currentPageData);
?>

4. Show results

 <?php
echo "当前Page的最后一条数据: <br>";
echo "ID: " . $lastItemOnCurrentPage['id'] . " Name: " . $lastItemOnCurrentPage['name'];
?>

5. Complete code example

 <?php
$data = [
    ['id' => 1, 'name' => 'Item 1'],
    ['id' => 2, 'name' => 'Item 2'],
    ['id' => 3, 'name' => 'Item 3'],
    ['id' => 4, 'name' => 'Item 4'],
    ['id' => 5, 'name' => 'Item 5'],
    ['id' => 6, 'name' => 'Item 6'],
    ['id' => 7, 'name' => 'Item 7'],
    ['id' => 8, 'name' => 'Item 8'],
    ['id' => 9, 'name' => 'Item 9'],
    ['id' => 10, 'name' => 'Item 10']
];

$perPage = 3;
$totalItems = count($data);
$totalPages = ceil($totalItems / $perPage);
$currentPage = 4;

$startIndex = ($currentPage - 1) * $perPage;
$endIndex = min($startIndex + $perPage, $totalItems);

$currentPageData = array_slice($data, $startIndex, $endIndex - $startIndex);
$lastItemOnCurrentPage = end($currentPageData);

echo "当前Page的最后一条数据: <br>";
echo "ID: " . $lastItemOnCurrentPage['id'] . " Name: " . $lastItemOnCurrentPage['name'];
?>

Summarize

By using PHP's end() function, we can easily get the last record in the paged data. In actual development, the end() function is a very practical tool that can help us quickly access the last element in an array, especially suitable for previewing the last page data in a pagination system.

If you want to know more about the implementation methods of the paging system, you can refer to other paging-related PHP functions and technologies, and combine the end() function to improve the efficiency and readability of the code.