php - 如何处理分页中当前页面之前出现的页数?

标签 php html css

这是我的代码:

// 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;

enter image description here

$_GET['page'] = 1;

enter image description here

$_GET['page'] = 4;

enter image description here


如您所见,第一个示例看起来不错。

第二个不错(如果页数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/

相关文章:

php - 使用 CronTab 运行 php 脚本

javascript - AJAX 表单脚本未执行

html - 如何在 css 中创建叠加层?

javascript - HTML CSS 增加 SVG 图标的描边/厚度

html - 如何在 Bootstrap 中将两个元素添加到一行中?

css - Webkit CSS 渲染问题

php - 如何在另一个 php.ini 文件中包含一个 php.ini 文件?

php - 如何使用 PHP 从 MySql 数据库中删除用户

html - iframe 是否需要宽度/高度?

jquery - 当我在调整大小的窗口中滚动半视差时出现空白