分页休息

时间:2015-09-26 16:24:21

标签: php pagination

我需要打破分页,以显示像123456 ...... 320 目前我的分页显示从1到320的所有记录以下是我用于分页的代码。我使用循环和if else语句来显示所有页面但我没有在哪里输入代码来中断。

### Pagination on All results ###
$mx_page = ceil($page_count/$ttl_rws_);
$nav_pg = '';
$self = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];

for($page = 1; $page <= $mx_page; $page++){
    if($page == $page_no){
        $nav_pg .= '<li class="active"><a href="#">'.$page.'</a></li>';
    } else{
        if(isset($_GET['page'])){
            $self_ = preg_replace("/page=\d+/", "page=".$page, $self);
        } else {
            $self_ = $self.'&page='.$page;
        }
        $nav_pg .= '<li><a href="'.$self_.'">'.$page.'</a></li>';
    }
}
if ($page_no > 1){
    $page = $page_no - 1;
    if(isset($_GET['page'])){
            $prv_page = '<li><a href="'.preg_replace("/page=\d+/", "page=".$page, $self).'">&laquo;</a></li>';
            $frst_page = '<li><a href="'.preg_replace("/page=\d+/", "page=1", $self).'">Frist</a></li>';
    } else {
            $prv_page = '<li><a href="'.$self.'&page='.$page.'">&laquo;</a></li>';
            $frst_page = '<li><a href="'.$self.'&page=1'.'">Frist</a></li>';
        }
} else {
    $prv_page = '';
    $frst_page = '';
}
if ($page_no < $mx_page) {
    $page = $page_no + 1;
    if(isset($_GET['page'])){
        $nxt_page = '<li><a href="'.preg_replace("/page=\d+/", "page=".$page, $self).'">&raquo;</a></li>';
        $lst_page = '<li><a href="'.preg_replace("/page=\d+/", "page=".$mx_page, $self).'">Last</a></li>';
    } else {
        $nxt_page = '<li><a href="'.$self.'&page='.$page.'">&raquo;</a></li>';
        $lst_page = '<li><a href="'.$self.'&page='.$mx_page.'">Last</a></li>';
    }
} else {
    $nxt_page = '';
    $lst_page = '';
}
echo $frst_page.$prv_page.$nav_pg.$nxt_page.$lst_page;

1 个答案:

答案 0 :(得分:0)

我会在for循环中执行此操作:

$maxButtons = 10;
$threshold = 20;
$lastButtons = 3;
$dots = " ... ";

for($page = 1; $page <= $mx_page; $page++){
    if($page == $page_no){
        $nav_pg .= '<li class="active"><a href="#">'.$page.'</a></li>';
    } else{


        if(isset($_GET['page'])){
            $self_ = preg_replace("/page=\d+/", "page=".$page, $self);
        } else {
            $self_ = $self.'&page='.$page;
        }

        /*
         * this should give something like
         * 1 2 3 4 5 6 7 8 9 10 ... 49 50
         * on 50 pages:
         * We have more than $threshold pages and we are behind
         * $maxButtons to show and we did not reach the $lastButtons value
         * so we only show some dots:
         */
        if ( $mx_page > $threshold && $page > $maxbuttons && 
                                         $page < ($mx_page - $lastButtons )
        {
            $nav_pg .= $dots;
            $dots = '';
        }
        else
            $nav_pg .= '<li><a href="'.$self_.'">'.$page.'</a></li>';
    }
}

这不是针对其他逻辑检查的,而是应该说明如何解决这个问题。