Digg style pagination


/ Published in: PHP
Save to your folder(s)

CSS:

<pre><code>div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;

font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;

color: #DDD;
}</code></pre>


Copy this code and paste it in your HTML
  1. function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
  2. {
  3. //defaults
  4. if(!$adjacents) $adjacents = 1;
  5. if(!$limit) $limit = 15;
  6. if(!$page) $page = 1;
  7. if(!$targetpage) $targetpage = "/";
  8.  
  9. //other vars
  10. $prev = $page - 1; //previous page is page - 1
  11. $next = $page + 1; //next page is page + 1
  12. $lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
  13. $lpm1 = $lastpage - 1; //last page minus 1
  14.  
  15. /*
  16.   Now we apply our rules and draw the pagination object.
  17.   We're actually saving the code to a variable in case we want to draw it more than once.
  18.   */
  19. $pagination = "";
  20. if($lastpage > 1)
  21. {
  22. $pagination .= "<div class=\"pagination\"";
  23. if($margin || $padding)
  24. {
  25. $pagination .= " style=\"";
  26. if($margin)
  27. $pagination .= "margin: $margin;";
  28. if($padding)
  29. $pagination .= "padding: $padding;";
  30. $pagination .= "\"";
  31. }
  32. $pagination .= ">";
  33.  
  34. //previous button
  35. if ($page > 1)
  36. $pagination .= "<a href=\"$targetpage$pagestring$prev\">&laquo; previous</a>";
  37. else
  38. $pagination .= "<span class=\"disabled\">&laquo; previous</span>";
  39.  
  40. //pages
  41. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
  42. {
  43. for ($counter = 1; $counter <= $lastpage; $counter++)
  44. {
  45. if ($counter == $page)
  46. $pagination .= "<span class=\"current\">$counter</span>";
  47. else
  48. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
  49. }
  50. }
  51. elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
  52. {
  53. //close to beginning; only hide later pages
  54. if($page < 1 + ($adjacents * 3))
  55. {
  56. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  57. {
  58. if ($counter == $page)
  59. $pagination .= "<span class=\"current\">$counter</span>";
  60. else
  61. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
  62. }
  63. $pagination .= "<span class=\"elipses\">...</span>";
  64. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
  65. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
  66. }
  67. //in middle; hide some front and some back
  68. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  69. {
  70. $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
  71. $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
  72. $pagination .= "<span class=\"elipses\">...</span>";
  73. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  74. {
  75. if ($counter == $page)
  76. $pagination .= "<span class=\"current\">$counter</span>";
  77. else
  78. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
  79. }
  80. $pagination .= "...";
  81. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
  82. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
  83. }
  84. //close to end; only hide early pages
  85. else
  86. {
  87. $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
  88. $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
  89. $pagination .= "<span class=\"elipses\">...</span>";
  90. for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
  91. {
  92. if ($counter == $page)
  93. $pagination .= "<span class=\"current\">$counter</span>";
  94. else
  95. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
  96. }
  97. }
  98. }
  99.  
  100. //next button
  101. if ($page < $counter - 1)
  102. $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next &raquo;</a>";
  103. else
  104. $pagination .= "<span class=\"disabled\">next &raquo;</span>";
  105. $pagination .= "</div>\n";
  106. }
  107.  
  108. return $pagination;
  109.  
  110. }

URL: http://www.strangerstudios.com/sandbox/pagination/diggstyle.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.