简体   繁体   English

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

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

I need this kind of pagination 我需要这种分页 在此处输入图片说明

with a small change 稍有变化

the pagination should never show more than 5 buttons/links ( not counting next/prev ) and if I am somewhere in mid of pagination it should show 分页永远不要显示超过5个按钮/链接(不计算next / prev),如果我在分页中间,应该显示

1 ... 5 6 7 ... 20 1 ... 5 6 7 ... 20

or 要么

1... 9 10 11 ... 20 1 ... 9 10 11 ... 20

if on last page 如果在最后一页

1... 17 18 19 20 1 ... 17 18 19 20

I started with this 我从这个开始

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

}

on first and last I get this 首先,我得到这个

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

but on my page 5 it shows this and number of links increases because of the wrong current , total , limit calc. 但是在我的第5页上却显示了这一点,并且由于错误的current,total,limit calc导致链接数量增加。

在此处输入图片说明

any help is appreciated! 任何帮助表示赞赏!

This is the schema. 这是架构。 You can adapt it with your html code, styles and variable: 您可以使用html代码,样式和变量对其进行调整:

$pages is the total page number, $page is the current page, $start is the first of pages “group”: $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;

I have added comments in code, because I think it is self-explanatory. 我在代码中添加了注释,因为我认为它是不言自明的。 Feel free to ask if you have doubt. 随时问您是否有疑问。

phpFiddle demo phpFiddle演示

Thnx to fusion3K for workaround but the fix for my version is to adjust the limit on specific page numbers 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