A tumblelog CMS built on AJAX, PHP and MySQL.

pagination.class.php 8.0KB

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