A tumblelog CMS built on AJAX, PHP and MySQL.

pagination.class.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. class pagination{
  3. /*
  4. Script Name: *Digg Style Paginator Class
  5. Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
  6. Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
  7. Script Version: 0.3
  8. Author: Victor De la Rocha
  9. Author URI: http://www.mis-algoritmos.com
  10. */
  11. /*Default values*/
  12. var $total_pages = null;
  13. var $limit = null;
  14. var $target = "";
  15. var $page = 1;
  16. var $adjacents = 2;
  17. var $showCounter = false;
  18. var $className = "pagination";
  19. var $parameterName = "page";
  20. var $urlF = false;//urlFriendly
  21. /*Buttons next and previous*/
  22. var $nextT = "Next";
  23. var $nextI = "&#187;"; //&#9658;
  24. var $prevT = "Previous";
  25. var $prevI = "&#171;"; //&#9668;
  26. /*****/
  27. var $calculate = false;
  28. #Total items
  29. function items($value){$this->total_pages = intval($value);}
  30. #how many items to show per page
  31. function limit($value){$this->limit = intval($value);}
  32. #Page to sent the page value
  33. function target($value){$this->target = $value;}
  34. #Current page
  35. function currentPage($value){$this->page = intval($value);}
  36. #How many adjacent pages should be shown on each side of the current page?
  37. function adjacents($value){$this->adjacents = intval($value);}
  38. #show counter?
  39. function showCounter($value=""){$this->showCounter=($value===true)?true:false;}
  40. #to change the class name of the pagination div
  41. function changeClass($value=""){$this->className=$value;}
  42. function nextLabel($value){$this->nextT = $value;}
  43. function nextIcon($value){$this->nextI = $value;}
  44. function prevLabel($value){$this->prevT = $value;}
  45. function prevIcon($value){$this->prevI = $value;}
  46. #to change the class name of the pagination div
  47. function parameterName($value=""){$this->parameterName=$value;}
  48. #to change urlFriendly
  49. function urlFriendly($value="%"){
  50. if(eregi('^ *$',$value)){
  51. $this->urlF=false;
  52. return false;
  53. }
  54. $this->urlF=$value;
  55. }
  56. var $pagination;
  57. function pagination(){}
  58. function show(){
  59. if(!$this->calculate)
  60. if($this->calculate())
  61. echo "<div class=\"$this->className\">$this->pagination</div>";
  62. }
  63. function get_pagenum_link($id){
  64. //if(strpos($this->target,'?')===false)
  65. if($this->urlF)
  66. return str_replace($this->urlF,$id,$this->target);
  67. else
  68. return "$this->target?$this->parameterName=$id";
  69. //else
  70. //return "$this->target&$this->parameterName=$id";
  71. }
  72. function calculate(){
  73. $this->pagination = "";
  74. $this->calculate == true;
  75. $error = false;
  76. if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
  77. //Es necesario especificar el comodin para sustituir
  78. echo "Especificaste un wildcard para sustituir, pero no existe en el target<br />";
  79. $error = true;
  80. }elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
  81. echo "Es necesario especificar en el target el comodin % para sustituir el número de página<br />";
  82. $error = true;
  83. }
  84. if($this->total_pages == null){
  85. echo "It is necessary to specify the <strong>number of pages</strong> (\$class->items(1000))<br />";
  86. $error = true;
  87. }
  88. if($this->limit == null){
  89. echo "It is necessary to specify the <strong>limit of items</strong> to show per page (\$class->limit(10))<br />";
  90. $error = true;
  91. }
  92. if($error)return false;
  93. $n = trim($this->nextT.' '.$this->nextI);
  94. $p = trim($this->prevI.' '.$this->prevT);
  95. /* Setup vars for query. */
  96. if($this->page)
  97. $start = ($this->page - 1) * $this->limit; //first item to display on this page
  98. else
  99. $start = 0; //if no page var is given, set start to 0
  100. /* Setup page vars for display. */
  101. if ($this->page == 0) $this->page = 1; //if no page var is given, default to 1.
  102. $prev = $this->page - 1; //previous page is page - 1
  103. $next = $this->page + 1; //next page is page + 1
  104. $lastpage = ceil($this->total_pages/$this->limit); //lastpage is = total pages / items per page, rounded up.
  105. $lpm1 = $lastpage - 1; //last page minus 1
  106. /*
  107. Now we apply our rules and draw the pagination object.
  108. We're actually saving the code to a variable in case we want to draw it more than once.
  109. */
  110. if($lastpage > 1){
  111. //anterior button
  112. if($this->page > 1)
  113. $this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\">$p</a>";
  114. else
  115. $this->pagination .= "<span class=\"disabled\">$p</span>";
  116. //pages
  117. if ($lastpage < 7 + ($this->adjacents * 2)){//not enough pages to bother breaking it up
  118. for ($counter = 1; $counter <= $lastpage; $counter++){
  119. if ($counter == $this->page)
  120. $this->pagination .= "<span class=\"current\">$counter</span>";
  121. else
  122. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  123. }
  124. }
  125. elseif($lastpage > 5 + ($this->adjacents * 2)){//enough pages to hide some
  126. //close to beginning; only hide later pages
  127. if($this->page < 1 + ($this->adjacents * 2)){
  128. for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
  129. if ($counter == $this->page)
  130. $this->pagination .= "<span class=\"current\">$counter</span>";
  131. else
  132. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  133. }
  134. $this->pagination .= "...";
  135. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  136. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  137. }
  138. //in middle; hide some front and some back
  139. elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
  140. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  141. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  142. $this->pagination .= "...";
  143. for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
  144. if ($counter == $this->page)
  145. $this->pagination .= "<span class=\"current\">$counter</span>";
  146. else
  147. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  148. $this->pagination .= "...";
  149. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  150. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  151. }
  152. //close to end; only hide early pages
  153. else{
  154. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  155. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  156. $this->pagination .= "...";
  157. for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
  158. if ($counter == $this->page)
  159. $this->pagination .= "<span class=\"current\">$counter</span>";
  160. else
  161. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  162. }
  163. }
  164. //siguiente button
  165. if ($this->page < $counter - 1)
  166. $this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\">$n</a>";
  167. else
  168. $this->pagination .= "<span class=\"disabled\">$n</span>";
  169. if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
  170. }
  171. return true;
  172. }
  173. }
  174. ?>