简体   繁体   中英

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

1 ... 5 6 7 ... 20

or

1... 9 10 11 ... 20

if on last page

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.

在此处输入图片说明

any help is appreciated!

This is the schema. You can adapt it with your html code, styles and variable:

$pages is the total page number, $page is the current page, $start is the first of pages “group”:

/* 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

Thnx to fusion3K for workaround but the fix for my version is to adjust the limit on specific page numbers

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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