ygeorgiev

Pagination Script

Feb 2nd, 2013
353
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 1 0
  1. <?php
  2. /* Pagination script v0.1
  3.  * Author: Yasen Georgiev
  4.  * Web site: http://ygeorgiev.com/
  5.  * License: MIT - http://ygeorgiev.com/licenses/MIT-LICENSE
  6. */
  7.  
  8. $commentsPerPage = 5; // Items per page
  9. $currentPage = (int)$_GET['p']; // Your pagination's GET parameter
  10. $getNumRows = mysql_num_rows(mysql_query('SELECT `column` FROM `table`')); // Set your Query to get count of the results in table
  11. $totalPages = $getNumRows/$commentsPerPage;
  12. $intvalPages = intval($totalPages);
  13. $lastPage = ceil($getNumRows / $commentsPerPage);
  14. $paginationPath = 'page.php?id='.(int)$_GET['id'].'&p='; //Set your URL path
  15.  
  16. if($totalPages-$intvalPages>0) {
  17.     $totalPages = $intvalPages+1;
  18. }
  19.  
  20. if($currentPage <= 1 || $currentPage>$totalPages) {
  21.     $start = 0;
  22.     $end = $commentsPerPage;
  23.     $currentPage = "1";
  24. } else {
  25.     $start = ($currentPage-1)*$commentsPerPage+1;
  26.     $end = $currentPage*$commentsPerPage;
  27. }
  28. if($lastPage>1) {
  29.     if($currentPage > 1) {
  30.         $pagesNum .= '<a href="'.$paginationPath.($currentPage-1).'">« Previous page</a> ';
  31.     }
  32.     if($lastPage <= 10) {
  33.         for ($i = 1; $i <= $lastPage; $i++) {
  34.             $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
  35.         }
  36.     } else {
  37.         if($currentPage <= 5) {
  38.             for($i = 1; $i <= 10; $i++) {
  39.                 $pagesNum .= ' | <a href="'.$paginationPath.$i.'">'.$i.'</a> ';
  40.             }
  41.             $pagesNum .= ' | ... | <a href="'.$paginationPath.$lastPage.'">'.$lastPage.'</a> | ';
  42.         } elseif($currentPage > 5 && $currentPage < ($lastPage-5)) {
  43.             $pagesNum .= ' | <a href="'.$paginationPath.'1">1</a> | ... | ';
  44.             for($i = $currentPage-3; $i <= $currentPage+3; $i++) {
  45.                 $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
  46.             }
  47.             $pagesNum .= ' ... | <a href="'.$paginationPath.$lastPage.'">'.$lastPage.'</a> | ';
  48.         } elseif($currentPage >= ($lastPage-5)) {
  49.             $pagesNum .= ' | <a href="'.$paginationPath.'1">1</a> | ... | ';
  50.             if($currentPage < ($lastPage-2)) {
  51.                 for($i = $currentPage-3; $i <= $currentPage + 3; $i++) {
  52.                     $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
  53.                 }
  54.             } else {
  55.                 for($i = $lastPage - 5; $i <= $lastPage; $i++) {
  56.                     $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     if($currentPage < $lastPage) {
  62.         $pagesNum .= ' <a href="'.$paginationPath.($currentPage+1).'">Next page »</a>';
  63.     }
  64.     $nomeration = str_replace('<a href="'.$paginationPath.$currentPage.'">'.$currentPage.'</a>','<b>'.$currentPage.'</b>',$pagesNum);
  65. }
  66.  
  67. echo $nomeration; //Show the pagination
  68. ?>
Advertisement
Comments
Add Comment
Please, Sign In to add comment