php - How to echo "..." after number 9 and echo the last two numbers after "..." for pagination? -


how echo 3 dots "..." after specific number (e.g., 9) , echo last 2 numbers after 3 dots "..." (e.g., 57 , 58) pagination using mysqli , php?

here's code:

<?php          $output = "";         $tpages = ceil($engine->numrows("select null `cms_articles_comments` `article_id` = '" . $id . "'") / 10);          if ($page == 1)         {             $output .= '<button type="button" class="btn btn-info pull-left gototop" disabled><i class="fa fa-arrow-left"></i> previous</button>';         }         else         {             $output .= '<a href="/articles/' . $seo . '&p=' . ($page - 1) . '#comments" ng-click="progress()" class="btn btn-info pull-left gototop"><i class="fa fa-arrow-left"></i> previous</a>';         }          if ($page >= $tpages)         {             $output .= '<button type="button" class="btn btn-info pull-right gototop" disabled>next <i class="fa fa-arrow-right"></i></button>';         }         else         {             $output .= '<a href="/articles/' . $seo . '&p=' . ($page + 1) . '#comments" ng-click="progress()" class="btn btn-info pull-right gototop">next <i class="fa fa-arrow-right"></i></a>';         }          $output .= '<div class="fpagination fpagination-centered"><ul>';         for($i = 1; $i <= $tpages; $i++)         {             if ($i == $page)             {                 $output .= '<li class="active"><a href="/articles/' . $seo . '&p=' . $i . '#comments" ng-click="progress()" class="gototop">' . $i . '</a></li>';             }             else             {                 $output .= '<li><a href="/articles/' . $seo . '&p=' . $i . '#comments" ng-click="progress()" class="gototop">' . $i . '</a></li>';             }         }          $output .= '</ul></div>';                    echo $output;      ?> 

with code provided above, this:

here

i want pagination list show this:

here

prntscr.com/a0azua, prntscr.com/a0ay1s

thanks in advance.

the display system looking more complex seems, because if have 58 pages , current page 53, you'll want show three-dot button before page 53 , not after, this:

1 2 ... 50 51 52 [53] 54 55 56 57 58 

and when current page 20, you'll want them appear on both sides:

1 2 ... 18 19 [20] 21 22 23 ... 57 58 

in general, want system whereby maximum number of labels/buttons output (it seems 12 in example), counting three-dot labels.

it user-friendly show @ least 2 pages preceding current page, , 2 following it.

you write function generate list of page numbers follow these rules, , use number 0 denote three-dot button:

// returns array of $max_length (or less) page numbers // 0 in returned array denotes gap in series. // parameters: //   $tpages:     total number of pages //   $page:       current page //   $max_length: maximum size of returned array function getpagelist($tpages, $page, $max_length) {     if ($tpages <= $max_length) {         // no breaks in list         return range(1, $tpages);     }     if ($page <= $max_length - 5) {         // no break on left of page         return array_merge(range(1, $max_length-3), array(0, $tpages-1, $tpages));     }     if ($page >= $tpages - $max_length + 6) {         // no break on right of page         return array_merge(array(1, 2, 0), range($tpages - $max_length + 4, $tpages));     }     // breaks on both sides     $size = $max_length - 6;     $first = $page - floor(($size-1)/2);     return array_merge(array(1, 2, 0), range($first, $first + $size - 1),                         array(0, $tpages-1, $tpages)); } 

if call function this:

getpagelist(58, 53, 12) 

it return this:

[1, 2, 0, 50, 51, 52, 53, 54, 55, 56, 57, 58] 

or this, changing current page number:

getpagelist(58, 20, 12) 

it return this:

[1, 2, 0, 18, 19, 20, 21, 22, 23, 0, 57, 58] 

notice zeroes, place holders three-dot buttons.

now difficult part has been done. can call function existing code without having change much. instead of for loop, you'll use foreach on output of above function. in loop you'll have test on zero, can output three-dot label:

$output = ""; // first part of code getting $tpages ,  // generating prev/next buttons comes here: not change // ... //  $output .= '<div class="fpagination fpagination-centered"><ul>'; foreach (getpagelist($tpages, $page, 12) $i) {     if ($i == $page)     {         $output .= '<li class="active"><a href="/articles/' . $seo . '&p=' . $i .                       '#comments" ng-click="progress()" class="gototop">' .                       $i . '</a></li>';     }     else if ($i == 0)     {         $output .= '<li>&hellip;</li>';     }     else     {         $output .= '<li><a href="/articles/' . $seo . '&p=' . $i . '#comments"                      ng-click="progress()" class="gototop">' . $i . '</a></li>';     } } $output .= '</ul></div>';            echo $output; 

notice last argument getpagelist function 12. can configure number needs. depends on how wide want output become.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -