Advertisement
Guest User

PHP Pagination

a guest
Feb 2nd, 2017
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | None | 0 0
  1. <?php
  2.     // Function to Paginate the records from any table.
  3.     function paginator($dataSelection, $table, $where1, $where2, $order, $pageTo, $inPage) {
  4.         global $con, $stmt, $paginationCtrls, $lastPage;
  5.         // Getting the rows number of the table
  6.         $stmt = $con->prepare("SELECT * FROM $table $where1");
  7.       $stmt->execute();
  8.       // Assigning rows number of the specified table
  9.       $rowCount = count($stmt->fetchAll());
  10.  
  11.         // Get the page number and make sure its an interger
  12.         $p = $_GET['page'];
  13.         $page = isset($p) ? preg_replace('#[^0-9]#', '', $p) : 1;
  14.         // Per Page records
  15.         $perPage = $inPage;
  16.         // Last Page Number
  17.         $lastPage = ceil($rowCount / $perPage);
  18.         /*
  19.             Checking if the page number is less than 1 then make it one, and if the page number is greater than the last page number, then make it equal to the last page number.
  20.         */
  21.         if($page < 1) {
  22.             $page = 1;
  23.         } elseif($page > $lastPage) {
  24.             $page = $lastPage;
  25.         }
  26.         // This sets the range of rows to $stmt for the chosen $page.
  27.         $limit = 'LIMIT ' . ($page - 1) * $perPage . ',' . $perPage;
  28.         // This is your $stmt again, it is for grabbing just one page worth of rows by applying $limit to it.
  29.         $stmt = $con->prepare("SELECT $dataSelection FROM $table $where2 ORDER BY $order $limit");
  30.         // Establish the $paginationCtrls variable.
  31.         $paginationCtrls = '';
  32.         // Show the pagination if the rows numbers is worth displaying
  33.         if($lastPage != 1) {
  34.             /*
  35.                 First we check if we are on page one. If yes then we don't need a link to
  36.                 the previous page or the first page so we do nothing. If we aren't then we
  37.                 generate links to the first page, and to the previous pages.
  38.         */
  39.             if ($page > 1) {
  40.                 $previous = $page - 1;
  41.                 // Concatenate the link to the variable
  42.                 $paginationCtrls .= '
  43.                 <li>
  44.                     <a href="'.$pageTo.'page='.$previous.'" aria-label="Previous">
  45.                         <span aria-hidden="true">&laquo;</span>
  46.                     </a>
  47.                 </li>';
  48.                 // Render clickable number links that should appear on the left of the target (current) page number
  49.                 for($i = $page - 4; $i < $page; $i++) {
  50.                     if($i > 0) {
  51.                         // Concatenate the link to the variable
  52.                         $paginationCtrls .= '
  53.                         <li>
  54.                             <a href="'.$pageTo.'page='.$i.'">
  55.                                 '.$i.'
  56.                             </a>
  57.                         </li>';
  58.                     }
  59.                 }
  60.             }
  61.             // Render the target (current) page number, but without it being a clickable link
  62.             // Concatenate the link to the variable
  63.             $paginationCtrls .= '
  64.             <li class="active">
  65.                 <a>
  66.                     '.$page.'
  67.                 </a>
  68.             </li>';
  69.             // Render clickable number links that should appear on the right of the target (current) page number
  70.             for($i = $page + 1; $i <= $lastPage; $i++) {
  71.                 // Concatenate the link to the variable
  72.                 $paginationCtrls .= '
  73.                 <li>
  74.                     <a href="'.$pageTo.'page='.$i.'">
  75.                         '.$i.'
  76.                     </a>
  77.                 </li>';
  78.                 // if the loop runs for tims then break (stop) it.
  79.                 if($i >= $page + 4) {
  80.                     break;
  81.                 }
  82.             }
  83.             // This does the same as above, only checking if we are on the last page, if not then generating the "Next"
  84.         if ($page != $lastPage) {
  85.         $next = $page + 1;
  86.         // Concatenate the link to the variable
  87.         $paginationCtrls .= '
  88.        <li>
  89.             <a href="'.$pageTo.'page='.$next.'" aria-label="Next">
  90.                 <span aria-hidden="true">
  91.                     &raquo;
  92.                 </span>
  93.             </a>
  94.        </li>';
  95.         }
  96.         }
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement