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

Here is my code:

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

The result of it doesn't look good all the times. Here are some examples:

$_GET['page'] = 0;

enter image description here

$_GET['page'] = 1;

enter image description here

$_GET['page'] = 4;

enter image description here


As you see, the first example looks good.

The second one is not bad (it would be better if the number of page 1 been visible instead of ...)

The third one is awful. It's totally wrong.

I really don't know how can I fix the problem. Do you have any idea?

Noted: the language I use is right-to-left and قبلی means prev, and بعدی means next.

If you don't care how many links show before/after the current page, you could use the following:

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

Edit

And if you do care how many adjacent links show before / after the current page, you could use the following, changing $adjacent accordingly.

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