A tumblelog CMS built on AJAX, PHP and MySQL.

pagination.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. Digg-Style Pagination
  4. Autor: jason (http://www.strangerstudios.com/blog/2006/12/29/pagination-update/)
  5. Update: Victor De la Rocha (http://www.mis-algoritmos.com/2006/11/23/paginacion-al-estilo-digg-y-sabrosus/)
  6. */
  7. function pagination($total_pages,$limit,$page=1,$pagina="default.php",$adjacents=2){
  8. /*
  9. $adjacents -> How many adjacent pages should be shown on each side?
  10. $total_pages -> items
  11. $limit -> how many items to show per page
  12. $page -> Current page
  13. $pagina -> Pagina
  14. */
  15. if(!is_array($pagina)&&!strstr($pagina,"?"))
  16. $pagina = $pagina."?";
  17. /* Setup vars for query. */
  18. if($page)
  19. $start = ($page - 1) * $limit; //first item to display on this page
  20. else
  21. $start = 0; //if no page var is given, set start to 0
  22. /* Setup page vars for display. */
  23. if ($page == 0) $page = 1; //if no page var is given, default to 1.
  24. $prev = $page - 1; //previous page is page - 1
  25. $next = $page + 1; //next page is page + 1
  26. $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
  27. $lpm1 = $lastpage - 1; //last page minus 1
  28. /*
  29. Now we apply our rules and draw the pagination object.
  30. We're actually saving the code to a variable in case we want to draw it more than once.
  31. */
  32. $pagination = "";
  33. if($lastpage > 1)
  34. {
  35. $pagination .= "<div class=\"pagination\">";
  36. //previous button
  37. if ($page > 1){
  38. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$prev,$pagina[0]):$pagina."&page=$prev";
  39. $pagination.= "<a href=\"".$pagina_uf."\"> Previous</a>";
  40. }else
  41. $pagination.= "<span class=\"disabled\"> Previous</span>";
  42. //pages
  43. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
  44. {
  45. for ($counter = 1; $counter <= $lastpage; $counter++)
  46. {
  47. if ($counter == $page)
  48. $pagination.= "<span class=\"current\">$counter</span>";
  49. else{
  50. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina."&page=$counter";
  51. $pagination.= "<a href=\"".$pagina_uf."\">$counter</a>";
  52. }
  53. }
  54. }
  55. elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
  56. {
  57. //close to beginning; only hide later pages
  58. if($page < 1 + ($adjacents * 2))
  59. {
  60. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  61. {
  62. if ($counter == $page)
  63. $pagination.= "<span class=\"current\">$counter</span>";
  64. else{
  65. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina."&page=$counter";
  66. $pagination.= "<a href=\"".$pagina_uf."\">$counter</a>";
  67. }
  68. }
  69. $pagination.= "...";
  70. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$lpm1,$pagina[0]):$pagina."&page=$lpm1";
  71. $pagination.= "<a href=\"".$pagina_uf."\">$lpm1</a>";
  72. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina."&page=$counter";
  73. $pagination.= "<a href=\"".$pagina_uf."\">$lastpage</a>";
  74. }
  75. //in middle; hide some front and some back
  76. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  77. {
  78. $pagina_uf = is_array($pagina)?str_replace($pagina[1],1,$pagina[0]):$pagina."&page=1";
  79. $pagination.= "<a href=\"".$pagina_uf."\">1</a>";
  80. $pagina_uf = is_array($pagina)?str_replace($pagina[1],2,$pagina[0]):$pagina."&page=2";
  81. $pagination.= "<a href=\"".$pagina_uf."\">2</a>";
  82. $pagination.= "...";
  83. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  84. {
  85. if ($counter == $page)
  86. $pagination.= "<span class=\"current\">$counter</span>";
  87. else{
  88. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina."&page=$counter";
  89. $pagination.= "<a href=\"".$pagina_uf."\">$counter</a>";
  90. }
  91. }
  92. $pagination.= "...";
  93. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$lpm1,$pagina[0]):$pagina."&page=$lpm1";
  94. $pagination.= "<a href=\"".$pagina_uf."\">$lpm1</a>";
  95. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$lastpage,$pagina[0]):$pagina."&page=$lastpage";
  96. $pagination.= "<a href=\"".$pagina_uf."\">$lastpage</a>";
  97. }
  98. //close to end; only hide early pages
  99. else
  100. {
  101. $pagina_uf = is_array($pagina)?str_replace($pagina[1],1,$pagina[0]):$pagina."&page=1";
  102. $pagination.= "<a href=\"".$pagina_uf."&page=1\">1</a>";
  103. $pagina_uf = is_array($pagina)?str_replace($pagina[1],2,$pagina[0]):$pagina."&page=2";
  104. $pagination.= "<a href=\"".$pagina_uf."&page=2\">2</a>";
  105. $pagination.= "...";
  106. for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
  107. {
  108. if ($counter == $page)
  109. $pagination.= "<span class=\"current\">$counter</span>";
  110. else{
  111. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina."&page=$counter";
  112. $pagination.= "<a href=\"".$pagina_uf."\">$counter</a>";
  113. }
  114. }
  115. }
  116. }
  117. //next button
  118. if ($page < $counter - 1) {
  119. $pagina_uf = is_array($pagina)?str_replace($pagina[1],$next,$pagina[0]):$pagina."&page=$next";
  120. $pagination.= "<a href=\"".$pagina_uf."\">Next </a>";
  121. }
  122. else
  123. $pagination.= "<span class=\"disabled\">Next </span>";
  124. $pagination.= "</div>\n";
  125. }
  126. return $pagination;
  127. }
  128. ?>