【问题标题】:Php pagination with links range. Need help adjusting the loop带有链接范围的 PHP 分页。需要帮助调整循环
【发布时间】:2016-07-07 05:31:42
【问题描述】:

我需要这种分页

稍作改动

分页不应显示超过 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 计算,链接数量增加了。

感谢任何帮助!

【问题讨论】:

    标签: php for-loop pagination


    【解决方案1】:

    这是架构。您可以使用您的 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;
    

    我在代码中添加了 cmets,因为我认为它是不言自明的。如有疑问,请随时询问。

    phpFiddle demo

    【讨论】:

    • 没有问题,但那个速记芽,一个迷宫,没有难过的感觉。代码有效,会尽量适应我的功能。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 2018-07-24
      相关资源
      最近更新 更多