如何处理在分页中当前页面之前显示的页数?

时间:2017-05-23 04:08:32

标签: 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بعدی表示{ {1}}。

1 个答案:

答案 0 :(得分:1)

如果您不关心在当前页面之前/之后显示的链接数量,您可以使用以下内容:

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