Guest User

Untitled

a guest
Oct 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. class Paginator
  2. {
  3. public $items_count = 0;
  4. public $page_size = 0;
  5. public $page_number = 0;
  6.  
  7. public $pages_count = 0;
  8.  
  9. public $sql_limit_from = 0;
  10. public $sql_limit_count = 0;
  11.  
  12. public $array_start_index = 0;
  13. public $array_end_index = 0;
  14.  
  15. public $page_numbers = array();
  16. public $active_page_number = 0;
  17.  
  18.  
  19. public function __construct($iItemsCount, $iPageSize, $iPageNumber)
  20. {
  21. $this->items_count = $iItemsCount;
  22. $this->page_size = $iPageSize;
  23. $this->page_number = $iPageNumber;
  24.  
  25. $this->_compute_pages_count();
  26.  
  27. $this->_compute_array_indices();
  28. $this->_compute_sql_limit();
  29.  
  30. $this->_make_page_numbers();
  31. $this->active_page_number = $iPageNumber;
  32.  
  33. }
  34.  
  35. private function _compute_pages_count()
  36. {
  37. $this->pages_count = (int) ($this->items_count / $this->page_size);
  38.  
  39. if ($this->items_count % $this->page_size > 0)
  40. {
  41. $this->pages_count += 1;
  42. }
  43. }
  44.  
  45. private function _compute_array_indices()
  46. {
  47. $preliminary_array_start_index = $this->page_size * ($this->page_number - 1);
  48.  
  49. if ($preliminary_array_start_index < $this->items_count)
  50. {
  51. $this->array_start_index = $preliminary_array_start_index;
  52. }
  53. else
  54. {
  55. $this->array_start_index = 0; //DO WELL . IMPLEMENT GOOD NUMBERS INDICATOR?
  56. }
  57.  
  58. $preliminary_array_end_index = $this->page_size * ($this->page_number - 1) + $this->page_size - 1;
  59.  
  60. if ($preliminary_array_end_index < $this->items_count)
  61. {
  62. $this->array_end_index = $preliminary_array_end_index;
  63. }
  64. else
  65. {
  66. $this->array_end_index = $this->items_count - 1;
  67. }
  68.  
  69. }
  70.  
  71. private function _compute_sql_limit()
  72. {
  73. $this->sql_limit_from = $this->page_size * ($this->page_number - 1);
  74. $this->sql_limit_count = $this->page_size;
  75. }
  76.  
  77. private function _make_page_numbers()
  78. {
  79. $this->page_numbers = array();
  80.  
  81. for ($number = 1; $number <= $this->pages_count; $number += 1)
  82. {
  83. $this->page_numbers[] = $number;
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment