In web development, paging is one of the common features. Whether it is displaying article lists, product catalogs, or user reviews, pagination is an efficient solution to avoid slow page loading due to excessive data loading at one time.
PHP provides a very practical function array_slice() , which can easily extract a portion of elements from an array. Combining this function, we can easily implement a basic array paging navigation function. This article will take you to implement it step by step.
The basic syntax of array_slice() is as follows:
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
$array : original array
$offset : Start position (start from 0)
$length : optional parameter, number of elements returned
$preserve_keys : Whether to retain the original key name, default to false
Suppose we have an array containing several article titles:
$articles = [
"Article 1", "Article 2", "Article 3", "Article 4", "Article 5",
"Article 6", "Article 7", "Article 8", "Article 9", "Article 10"
];
We want 3 articles to be displayed per page.
First, we need to know which page we are currently on, and then calculate the corresponding offset based on the page number.
<?php
$articles = [
"Article 1", "Article 2", "Article 3", "Article 4", "Article 5",
"Article 6", "Article 7", "Article 8", "Article 9", "Article 10"
];
$perPage = 3;
$totalArticles = count($articles);
$totalPages = ceil($totalArticles / $perPage);
// Get the current page number
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
if ($page > $totalPages) $page = $totalPages;
// Calculate the offset of the current page
$offset = ($page - 1) * $perPage;
// use array_slice Get the data of the current page
$currentPageArticles = array_slice($articles, $offset, $perPage);
?>
Next, we output the data of the current page to the page:
<ul>
<?php foreach ($currentPageArticles as $article): ?>
<li><?php echo htmlspecialchars($article); ?></li>
<?php endforeach; ?>
</ul>
We also need to add a page turn link at the bottom of the page:
<div class="pagination">
<?php if ($page > 1): ?>
<a href="https://gitbox.net/pagination.php?page=<?php echo $page - 1; ?>">Previous page</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<?php if ($i == $page): ?>
<strong><?php echo $i; ?></strong>
<?php else: ?>
<a href="https://gitbox.net/pagination.php?page=<?php echo $i; ?>"><?php echo $i; ?></a>
<?php endif; ?>
<?php endfor; ?>
<?php if ($page < $totalPages): ?>
<a href="https://gitbox.net/pagination.php?page=<?php echo $page + 1; ?>">Next page</a>
<?php endif; ?>
</div>
Through array_slice() and some basic paging logic, we can add paging function to any array content. This method is suitable for scenarios where the data volume is small and no database operation is required, such as static content lists, cached data or configuration item displays, etc. For larger data, it is recommended to use the database's LIMIT clause for pagination query.
Mastering array_slice() is an important step in building efficient PHP applications. Hope this article is helpful to you!