dongzhi4470 2017-05-23 04:08
浏览 54

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

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.

  • 写回答

1条回答 默认 最新

  • duanjiu6697 2017-05-23 05:45
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看