PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/paginator.class.php

http://showslow.googlecode.com/
PHP | 96 lines | 86 code | 9 blank | 1 comment | 20 complexity | 5233e9647e671eb24930fed3866b63a7 MD5 | raw file
  1. <?php
  2. class Paginator{
  3. var $items_per_page;
  4. var $items_total;
  5. var $current_page;
  6. var $num_pages;
  7. var $mid_range;
  8. var $return;
  9. var $default_ipp = 50;
  10. var $querystring;
  11. function Paginator()
  12. {
  13. $this->current_page = 1;
  14. $this->mid_range = 7;
  15. $this->items_per_page = $this->default_ipp;
  16. }
  17. function paginate($base)
  18. {
  19. $this->num_pages = ceil($this->items_total/$this->items_per_page);
  20. $this->current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1; // must be numeric > 0
  21. if($this->current_page < 1 Or !is_numeric($this->current_page)) $this->current_page = 1;
  22. if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
  23. $prev_page = $this->current_page-1;
  24. $next_page = $this->current_page+1;
  25. if($_GET)
  26. {
  27. foreach($_GET as $key => $value)
  28. {
  29. if ($key != "page") {
  30. $this->querystring .= '&' . urlencode($key) . '=' . urlencode($value);
  31. }
  32. }
  33. }
  34. if($this->num_pages > 10)
  35. {
  36. $this->return = ($this->current_page != 1 And $this->items_total >= 10)
  37. ? '<a class="paginate" href="' . htmlentities($base) .
  38. '?page='.urlencode($prev_page) .
  39. $this->querystring . '">&laquo; Previous</a> '
  40. : '<span class="inactive" href="#">&laquo; Previous</span> ';
  41. $this->start_range = $this->current_page - floor($this->mid_range/2);
  42. $this->end_range = $this->current_page + floor($this->mid_range/2);
  43. if($this->start_range <= 0)
  44. {
  45. $this->end_range += abs($this->start_range)+1;
  46. $this->start_range = 1;
  47. }
  48. if($this->end_range > $this->num_pages)
  49. {
  50. $this->start_range -= $this->end_range-$this->num_pages;
  51. $this->end_range = $this->num_pages;
  52. }
  53. $this->range = range($this->start_range,$this->end_range);
  54. for($i=1;$i<=$this->num_pages;$i++)
  55. {
  56. if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
  57. // loop through all pages. if first, last, or in range, display
  58. if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
  59. {
  60. $this->return .= ($i == $this->current_page
  61. ? '<a title="Go to page '.htmlentities($i).' of $this->num_pages" class="current" href="#">'.htmlentities($i).'</a> '
  62. : '<a class="paginate" title="Go to page '.htmlentities($i).' of '.htmlentities($this->num_pages).'" href="'.$base.'?page='.htmlentities($i).$this->querystring.'">'.htmlentities($i).'</a> ');
  63. }
  64. if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
  65. }
  66. $this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10))
  67. ? '<a class="paginate" href="'.$base.'?page='.htmlentities($next_page).$this->querystring.'">Next &raquo;</a>'."\n"
  68. : '<span class="inactive" href="#">&raquo; Next</span>'."\n";
  69. }
  70. else
  71. {
  72. for($i=1;$i<=$this->num_pages;$i++)
  73. {
  74. $this->return .= ($i == $this->current_page)
  75. ? '<a class="current" href="#">'.htmlentities($i).'</a> '
  76. : '<a class="paginate" href="'.$base.'?page='.htmlentities($i).$this->querystring.'">'.htmlentities($i).'</a> ';
  77. }
  78. }
  79. }
  80. function display_pages()
  81. {
  82. if ($this->num_pages <= 1) {
  83. return '';
  84. }
  85. return $this->return;
  86. }
  87. }