Advertisement
Guest User

Php pagination

a guest
Jul 7th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. <?php
  2. /* Setup page vars for display. */
  3.     if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
  4.     $prev = $page - 1;                          //previous page is page - 1
  5.     $next = $page + 1;                          //next page is page + 1
  6.     $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
  7.     $lpm1 = $lastpage - 1;                      //last page minus 1
  8.    
  9.     /*
  10.         Now we apply our rules and draw the pagination object.
  11.         We're actually saving the code to a variable in case we want to draw it more than once.
  12.     */
  13.         $pagination = "";
  14.         if($lastpage > 1)
  15.         {  
  16.             $pagination .= "<div class=\"pagination\">";
  17.         //previous buttons
  18.             if ($page > 1)
  19.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=$prev\">previous</a>";
  20.             else
  21.                 $pagination.= "<a class='disabled'><buttons disabled>previous</buttons></a>";  
  22.            
  23.         //pages
  24.         if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
  25.         {  
  26.             for ($counter = 1; $counter <= $lastpage; $counter++)
  27.             {
  28.                 if ($counter == $page)
  29.                     $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
  30.                 else
  31.                     $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                
  32.             }
  33.         }
  34.         elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
  35.         {
  36.             //close to beginning; only hide later pages
  37.             if($page < 1 + ($adjacents * 2))       
  38.             {
  39.                 for ($counter = 1; $counter < 1 + ($adjacents * 2); $counter++)
  40.                 {
  41.                     if ($counter == $page)
  42.                         $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
  43.                     else
  44.                         $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                
  45.                 }
  46.                 $pagination.= "...";
  47.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  48.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
  49.             }
  50.             //in middle; hide some front and some back
  51.             elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  52.             {
  53.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=1\"> 1 </a>";
  54.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=2\"> 2 </a>";
  55.                 $pagination.= "...";
  56.                 for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  57.                 {
  58.                     if ($counter == $page)
  59.                         $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
  60.                     else
  61.                         $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                
  62.                 }
  63.                 $pagination.= "...";
  64.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  65.                 $pagination.= "<a class='buttons' href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
  66.             }
  67.             //close to end; only hide early pages
  68.             else
  69.             {
  70.                 $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
  71.                 $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
  72.                 $pagination.= "...";
  73.                 for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
  74.                 {
  75.                     if ($counter == $page)
  76.                         $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
  77.                     else
  78.                         $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                
  79.                 }
  80.             }
  81.         }
  82.        
  83.         //next buttons
  84.         if ($page < $counter - 1)
  85.             $pagination.= "<a class='buttons' href=\"$targetpage?page=$next\">next</a>";
  86.         else
  87.             $pagination.= "<a class='buttons'>next</a>";
  88.         $pagination.= "</div>\n";      
  89.     }
  90.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement