簡體   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