A tumblelog CMS built on AJAX, PHP and MySQL.

pagination.class.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 get_pagenum_link($id){
  65. //if(strpos($this->target,'?')===false)
  66. if($this->urlF)
  67. return str_replace($this->urlF,$id,$this->target);
  68. else
  69. return "$this->target?$this->parameterName=$id";
  70. //else
  71. //return "$this->target&$this->parameterName=$id";
  72. }
  73. function calculate(){
  74. $this->pagination = "";
  75. $this->calculate == true;
  76. $error = false;
  77. if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
  78. //Es necesario especificar el comodin para sustituir
  79. echo "Especificaste un wildcard para sustituir, pero no existe en el target<br />";
  80. $error = true;
  81. }elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
  82. echo "Es necesario especificar en el target el comodin % para sustituir el número de página<br />";
  83. $error = true;
  84. }
  85. if($this->total_pages == null){
  86. echo "It is necessary to specify the <strong>number of pages</strong> (\$class->items(1000))<br />";
  87. $error = true;
  88. }
  89. if($this->limit == null){
  90. echo "It is necessary to specify the <strong>limit of items</strong> to show per page (\$class->limit(10))<br />";
  91. $error = true;
  92. }
  93. if($error)return false;
  94. $n = trim($this->nextT.' '.$this->nextI);
  95. $p = trim($this->prevI.' '.$this->prevT);
  96. /* Setup vars for query. */
  97. if($this->page)
  98. $start = ($this->page - 1) * $this->limit; //first item to display on this page
  99. else
  100. $start = 0; //if no page var is given, set start to 0
  101. /* Setup page vars for display. */
  102. if ($this->page == 0) $this->page = 1; //if no page var is given, default to 1.
  103. $prev = $this->page - 1; //previous page is page - 1
  104. $next = $this->page + 1; //next page is page + 1
  105. $lastpage = ceil($this->total_pages/$this->limit); //lastpage is = total pages / items per page, rounded up.
  106. $lpm1 = $lastpage - 1; //last page minus 1
  107. /*
  108. Now we apply our rules and draw the pagination object.
  109. We're actually saving the code to a variable in case we want to draw it more than once.
  110. */
  111. if($lastpage > 1){
  112. //anterior button
  113. if($this->page > 1)
  114. $this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\">$p</a>";
  115. else
  116. $this->pagination .= "<span class=\"disabled\">$p</span>";
  117. //pages
  118. if ($lastpage < 7 + ($this->adjacents * 2)){//not enough pages to bother breaking it up
  119. for ($counter = 1; $counter <= $lastpage; $counter++){
  120. if ($counter == $this->page)
  121. $this->pagination .= "<span class=\"current\">$counter</span>";
  122. else
  123. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  124. }
  125. }
  126. elseif($lastpage > 5 + ($this->adjacents * 2)){//enough pages to hide some
  127. //close to beginning; only hide later pages
  128. if($this->page < 1 + ($this->adjacents * 2)){
  129. for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
  130. if ($counter == $this->page)
  131. $this->pagination .= "<span class=\"current\">$counter</span>";
  132. else
  133. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  134. }
  135. $this->pagination .= "...";
  136. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  137. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  138. }
  139. //in middle; hide some front and some back
  140. elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
  141. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  142. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  143. $this->pagination .= "...";
  144. for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
  145. if ($counter == $this->page)
  146. $this->pagination .= "<span class=\"current\">$counter</span>";
  147. else
  148. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  149. $this->pagination .= "...";
  150. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  151. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  152. }
  153. //close to end; only hide early pages
  154. else{
  155. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  156. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  157. $this->pagination .= "...";
  158. for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
  159. if ($counter == $this->page)
  160. $this->pagination .= "<span class=\"current\">$counter</span>";
  161. else
  162. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  163. }
  164. }
  165. //siguiente button
  166. if ($this->page < $counter - 1)
  167. $this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\">$n</a>";
  168. else
  169. $this->pagination .= "<span class=\"disabled\">$n</span>";
  170. if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
  171. }
  172. return true;
  173. }
  174. }
  175. ?>