What are the implementation methods of PHP array paging?

WBOY
Release: 2024-05-02 10:12:02
Original
1161 people have browsed it

PHP array paging can be achieved through the following methods: array slicing (array_slice()): split the array according to offset and length. External iterator (LimitIterator): Use an iterator to traverse the array and set the offset and length limit. Built-in function (array_chunk()): Splits an array into chunks of specified size. Local implementation: Custom functions implement the paging algorithm, including calculating the total number of pages, offsets and returning the paging array.

What are the implementation methods of PHP array paging?

How to implement array paging in PHP

Paging is a common requirement in web development. It can divide a large amount of data into smaller chunks, thereby improving Page loading speed and user experience. There are several ways to implement array pagination in PHP.

Array slice

This is the simplest method, using PHP's array_slice() function:

// 获取当前页
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;

// 每页显示的记录数
$perPage = 10;

// 计算偏移量
$offset = ($currentPage - 1) * $perPage;

// 分页后的数组
$paginatedArray = array_slice($array, $offset, $perPage);
Copy after login

External iterator

The standard class library in PHP provides a LimitIterator class for traversing arrays:

use IteratorIterator;
use LimitIterator;

// 创建外部迭代器
$limitIterator = new LimitIterator(new ArrayIterator($array), $offset, $perPage);

// 分页后的数组
$paginatedArray = [];
foreach ($limitIterator as $item) {
    $paginatedArray[] = $item;
}
Copy after login

Built-in functions

array_chunk() was introduced in PHP 7.1 and later versions ) function, which divides the array into chunks of a specified size:

// 分页后的数组
$paginatedArray = array_chunk($array, $perPage);
Copy after login

Local implementation

You can also use the function to implement your own paging algorithm:

function paginate(array $array, int $currentPage, int $perPage): array
{
    $totalPages = ceil(count($array) / $perPage);

    if ($currentPage < 1 || $currentPage > $totalPages) {
        return [];
    }

    $offset = ($currentPage - 1) * $perPage;

    return array_slice($array, $offset, $perPage);
}
Copy after login

Practical case

Suppose we have an array of $users, containing 100 users, and now we need to display 10 pieces of data on each page:

// 获取当前页
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;

// 分页后的用户数组
$paginatedUsers = paginate($users, $currentPage, 10);
Copy after login

Then, we can display the pagination on the page User data:

<ul>
    <?php foreach ($paginatedUsers as $user): ?>
        <li><?php echo $user['name']; ?></li>
    <?php endforeach; ?>
</ul>
Copy after login

Paging control can be implemented as needed, for example:

<nav aria-label="Pagination">
    <ul class="pagination">
        <?php if ($currentPage > 1): ?>
            <li class="page-item">
                <a class="page-link" href="<?php echo "?page=" . ($currentPage - 1); ?>">Previous</a>
            </li>
        <?php endif; ?>

        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <li class="page-item <?php echo ($currentPage == $i) ? 'active' : ''; ?>">
                <a class="page-link" href="<?php echo "?page=" . $i; ?>"><?php echo $i; ?></a>
            </li>
        <?php endfor; ?>

        <?php if ($currentPage < $totalPages): ?>
            <li class="page-item">
                <a class="page-link" href="<?php echo "?page=" . ($currentPage + 1); ?>">Next</a>
            </li>
        <?php endif; ?>
    </ul>
</nav>
Copy after login

The above is the detailed content of What are the implementation methods of PHP array paging?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template