这是我的代码:
// current page
$p = $_GET['page'];
// the number of all pages
$page_nums = 5;
// pagination should be created
if( $page_nums > 1 ) {
$data['pagination'] = '<div class="pagination_box">';
// backward btn
if ($p > 1) {
$data['pagination'] .= "<a class='pagination_backward' href=''>قبلی</a>";
$data['pagination'] .= '<span style="color:#848d95; margin:0px 10px;">…</span>';
}
$pagination_active = "pagination_active";
for($i = $p; $i <= $page_nums; $i++){
$data['pagination'] .= "<a class='$pagination_active' href=''>$i</a> ";
$pagination_active = '';
if ($i >= 2 && $i % 2) {
$data['pagination'] .= '<span style="color:#848d95; margin:0px 10px;">…</span>';
$data['pagination'] .= "<a href=''>$page_nums</a>";
break;
}
}
// forward btn
$data['pagination'] .= ($page_nums > $p) ? '<a class="pagination_backward" href="">بعدی</a>' : null;
它的结果看起来并不总是很好。以下是一些示例:
$_GET['page'] = 0;
$_GET['page'] = 1;
$_GET['page'] = 4;
如您所见,第一个示例看起来不错。
第二个不错(如果页数1
可见而不是...
会更好)
第三个很糟糕。这是完全错误的。
我真的不知道该如何解决这个问题。你有什么想法吗?
注意:我使用的语言是从右到左,قبلی
表示prev
,并且بعدی
表示下一个
。
最佳答案
如果您不关心当前页面之前/之后显示了多少个链接,您可以使用以下方法:
// Prepare current, total pages and pagination container
$current = isset($_GET['page']) ? intval($_GET['page']) : 1;
$total = 5;
$pagination = [];
// Check if pagination required
if ($total > 1) {
// Check if current page is not first page
if ($current > 1) {
$pagination[] = '<a class="pagination_backward" href="#">قبلی</a>';
$pagination[] = '<span style="color:#848d95; margin:0px 10px;">…</span>';
}
// Iterate through pages
for ($i = 1; $i <= $total; $i++) {
// Check if handling current page
if ($i === $current) {
$pagination[] = '<a class="pagination_active" href="#">' . $i . '</a>';
}
else {
$pagination[] = '<a href="#">' . $i . '</a>';
}
}
// Check if current page is not last page
if ($current < $total) {
$pagination[] = '<span style="color:#848d95; margin:0px 10px;">…</span>';
$pagination[] = '<a class="pagination_backward" href="#">بعدی</a>';
}
}
echo '<pre>';
var_dump($pagination);
echo '</pre>';
// $pagination = implode('', $pagination);
编辑
如果您确实关心在当前页面之前/之后显示多少个相邻链接,您可以使用以下内容,相应地更改 $adjacent
。
// Prepare current, total and adjacent pages and pagination container
$current = isset($_GET['page']) ? intval($_GET['page']) : 1;
$total = 5;
$adjacent = 2;
$pagination = [];
// Check if pagination required
if ($total > 1) {
// Check if current page is not first page
if ($current > 1) {
$pagination[] = '<a class="pagination_backward" href="#">قبلی</a>';
$pagination[] = '<span style="color:#848d95; margin:0px 10px;">…</span>';
}
// Prepare adjacent page delimiters
$min = max(1, $current - $adjacent);
$max = min($current + $adjacent, $total);
// Iterate through pages
for ($i = $min; $i <= $max; $i++) {
// Check if handling current page
if ($i === $current) {
$pagination[] = '<a class="pagination_active" href="#">' . $i . '</a>';
}
else {
$pagination[] = '<a href="#">' . $i . '</a>';
}
}
// Check if current page is not last page
if ($current < $total) {
$pagination[] = '<span style="color:#848d95; margin:0px 10px;">…</span>';
$pagination[] = '<a class="pagination_backward" href="#">بعدی</a>';
}
}
echo '<pre>';
var_dump($pagination);
echo '</pre>';
// $pagination = implode('', $pagination);
关于php - 如何处理分页中当前页面之前出现的页数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44125651/