Advertisement
ygeorgiev

Pagination Script

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