繁体   English   中英

php分页与链接范围。 需要帮助调整循环

[英]Php pagination with links range. Need help adjusting the loop

我需要这种分页 在此处输入图片说明

稍有变化

分页永远不要显示超过5个按钮/链接(不计算next / prev),如果我在分页中间,应该显示

1 ... 5 6 7 ... 20

要么

1 ... 9 10 11 ... 20

如果在最后一页

1 ... 17 18 19 20

我从这个开始

function pagination (){
    // prev link

    $html ='';
    $numpages = 20;
    $current_page = 1;
    $dotshow = true;

    if ($numpages != 1) {
        $html .='<span><i class="fa fa-angle-left"></i></span>';// prev


        for($i=1; $i <= $numpages; $i++){


            if ($i == 1 || $i == $numpages ||  ($i >= $current_page - 3 &&  $i <= $current_page + 3) ) {
                  $dotshow = true;
                  if ($i != $current_page){

                    $html .='<a class="pagination-link" href="#linkhere">';
                    $html .='<span> '.$i.'</span>';
                    $html .='</a>'; 

                  }else{
                    $html .='<span class="current">';
                    $html .='<span> '.$i.'</span>';
                    $html .='</span>';            

                  }
               }else if ( $dotshow ){

                   $dotshow = false;
                    $html .='<span class="dots">';
                    $html .='<span> ... </span>';
                    $html .='</span>';  
            }

        }
        $html .='<span><i class="fa fa-angle-right"></i></span>';// next
    }
    if($html !=''){
        return $html;
    }

}

首先,我得到这个

在此处输入图片说明 在此处输入图片说明

但是在我的第5页上却显示了这一点,并且由于错误的current,total,limit calc导致链接数量增加。

在此处输入图片说明

任何帮助表示赞赏!

这是架构。 您可以使用html代码,样式和变量对其进行调整:

$pages是总页数, $page是当前页, $start是“组”页面的第一页:

/* Set subgroup page start:                           */
if    ( $pages < 6        ) $start = 2;             
elseif( $page  < 3        ) $start = 2;             
elseif( $page  > $pages-3 ) $start = $pages - 3;    
else                        $start = $page  - 1;    

/* Compose line:                                      */
/* Page 1 (always):                                   */
$output = '[1]';
/* Initial separator:                                 */
if( $start > 2 ) $output .= '...';
/* Page subgroup: ends if reached +2 or pages-1       */
for( $i = $start; $i < $pages; $i++ ) 
{ 
    $output .= "[$i]"; 
    if( $i > ($start+1) ) break; 
}
/* Final separator:                                   */
if( $start < $pages - 3 ) $output .= '...';
/* Last page:                                         */
if( $pages > 1 ) $output .= "[$pages]";

/* Output:                                            */
echo $output;

我在代码中添加了注释,因为我认为它是不言自明的。 随时问您是否有疑问。

phpFiddle演示

Thnx到Fusion3K的解决方法,但我的版本的修复是调整特定页码的限制

function pagination (){

    $html ='';
    $numpages = 20;
    $current_page = 1;
    $dotshow = true;


    if( $current_page == 2 || $current_page == $numpages -1 ){

        $limit = 2;

    }else if($current_page >= 3 && $current_page != $numpages ){

        $limit = 1;

    }else{

        $limit = 3;
    }

    if ($numpages != 1) {
        $html .='<span><i class="fa fa-angle-left"></i></span>';// prev


        for($i=1; $i <= $numpages; $i++){


            if ($i == 1 || $i == $numpages ||  ($i >= $current_page - $limit &&  $i <= $current_page + $limit) ) {
                  $dotshow = true;
                  if ($i != $current_page){

                    $html .='<a class="pagination-link" href="#linkhere">';
                    $html .='<span> '.$i.'</span>';
                    $html .='</a>'; 

                  }else{
                    $html .='<span class="current">';
                    $html .='<span> '.$i.'</span>';
                    $html .='</span>';            

                  }
               }else if ( $dotshow ){

                   $dotshow = false;
                    $html .='<span class="dots">';
                    $html .='<span> ... </span>';
                    $html .='</span>';  
            }

        }
        $html .='<span><i class="fa fa-angle-right"></i></span>';// next
    }
    if($html !=''){
        return $html;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM