Suppose we have an array containing all articles in a certain category. The core of paging is to intercept the corresponding data fragment from the array based on the current page number and the number of pieces displayed on each page.
The paging parameters usually include:
$page : Current page number
$pageSize : Number of articles displayed per page
$offset : offset, calculated as ($page - 1) * $pageSize
Using array_slice , we can insert $pageSize articles starting with $offset from the article array to achieve pagination.
Below is a simple example showing how to implement article classification pagination using array_slice .
<?php
// Simulate all article arrays in categories
$articles = [
['id' => 1, 'title' => 'Article 1', 'category' => 'science and technology'],
['id' => 2, 'title' => 'Article 2', 'category' => 'science and technology'],
['id' => 3, 'title' => 'Article 3', 'category' => 'Life'],
['id' => 4, 'title' => 'Article 4', 'category' => 'science and technology'],
['id' => 5, 'title' => 'Article 5', 'category' => 'Life'],
['id' => 6, 'title' => 'Article 6', 'category' => 'science and technology'],
['id' => 7, 'title' => 'Article 7', 'category' => 'science and technology'],
];
// Select a category
$category = 'science and technology';
// Filter out current articles
$categoryArticles = array_filter($articles, function ($article) use ($category) {
return $article['category'] === $category;
});
// Rebuild the index,Easy to use array_slice
$categoryArticles = array_values($categoryArticles);
// Current page number,from URL Get,Default 1 Page
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
// 每Page显示文章数
$pageSize = 3;
// Calculate offset
$offset = ($page - 1) * $pageSize;
// 截取当前Page的文章
$currentPageArticles = array_slice($categoryArticles, $offset, $pageSize);
// 输出当前Page文章
foreach ($currentPageArticles as $article) {
echo "<h3>" . htmlspecialchars($article['title']) . "</h3>";
}
?>
Pagination not only needs to display content, but also provide users with navigation to switch pages. The total number of pages can be calculated based on the total number of articles and paged links can be generated dynamically.
<?php
$totalArticles = count($categoryArticles);
$totalPages = ceil($totalArticles / $pageSize);
for ($i = 1; $i <= $totalPages; $i++) {
if ($i == $page) {
echo "<strong>$i</strong> ";
} else {
echo "<a href=\"http://gitbox.net/yourcms?page=$i\">$i</a> ";
}
}
?>
Here we use http://gitbox.net as the sample domain name, which you can replace according to the actual situation.
Using PHP's array_slice function, it is very convenient to implement paging of array data, which is suitable for paging display of classified articles. The key is to correctly calculate the offset and slice the array after classification filtering. Coupled with simple pagination navigation, it can meet the needs of most CMSs for article classification pagination.
This method does not rely on database paging and is suitable for scenarios where data is small or data has been preloaded into an array. For large amounts of data, it is recommended to use pagination query of the database to improve efficiency.