A tumblelog CMS built on AJAX, PHP and MySQL.

pagination.class.php 11KB

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