1
votes

I need this kind of pagination enter image description here

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

enter image description here enter image description here

but on my page 5 it shows this and number of links increases because of the wrong current , total , limit calc.

enter image description here

any help is appreciated!

2

2 Answers

2
votes

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

0
votes

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

}