Guest User

Untitled

a guest
Aug 2nd, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <?php
  2. class NewsPagination {
  3. public $total = 0;
  4. public $page = 1;
  5. public $limit = 20;
  6. public $num_links = 8;
  7. public $url = '';
  8. public $text_first = '|&lt;';
  9. public $text_last = '&gt;|';
  10. public $text_next = '&gt;';
  11. public $text_prev = '&lt;';
  12.  
  13. public function render() {
  14. $total = $this->total;
  15.  
  16. if ($this->page < 1) {
  17. $page = 1;
  18. } else {
  19. $page = $this->page;
  20. }
  21.  
  22. if (!(int)$this->limit) {
  23. $limit = 10;
  24. } else {
  25. $limit = $this->limit;
  26. }
  27.  
  28. $num_links = $this->num_links;
  29. $num_pages = ceil($total / $limit);
  30.  
  31. $this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
  32.  
  33. $output = '<div class="pagination"><!--<a class="btn pagination__btn" href="#">Показать ещё<i class="icon-arrow-down"></i></a>--><div class="pagination__pages">';
  34.  
  35. if ($page > 1) {
  36. //$output .= '<a class="pagination__prev" href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '"><i class="icon-arrow-down"></i></a>';
  37.  
  38. if ($page - 1 === 1) {
  39. $output .= '<a class="pagination__prev" href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '"><i class="icon-arrow-down"></i></a>';
  40. } else {
  41. $output .= '<a class="pagination__prev" href="' . str_replace('{page}', $page - 1, $this->url) . '"><i class="icon-arrow-down"></i></a>';
  42. }
  43. }
  44.  
  45. if ($num_pages > 1) {
  46. if ($num_pages <= $num_links) {
  47. $start = 1;
  48. $end = $num_pages;
  49. } else {
  50. $start = $page - floor($num_links / 2);
  51. $end = $page + floor($num_links / 2);
  52.  
  53. if ($start < 1) {
  54. $end += abs($start) + 1;
  55. $start = 1;
  56. }
  57.  
  58. if ($end > $num_pages) {
  59. $start -= ($end - $num_pages);
  60. $end = $num_pages;
  61. }
  62. }
  63. $output .= '<div class="pagination__pages-list">';
  64. for ($i = $start; $i <= $end; $i++) {
  65. if($i == $end){
  66. $output .= '<div class="pagination__more"><span></span><span></span><span></span></div>';
  67. }
  68. if ($page == $i) {
  69. $output .= '<a class="pagination__pages-item active">' . $i . '</a>';
  70. } else {
  71. if ($i === 1) {
  72. $output .= '<a class="pagination__pages-item" href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '">' . $i . '</a>';
  73. } else {
  74. $output .= '<a class="pagination__pages-item" href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a>';
  75. }
  76. }
  77.  
  78. }
  79.  
  80. $output .= '</div>';
  81. }
  82.  
  83. if ($page < $num_pages) {
  84. $output .= '<a class="pagination__next" href="' . str_replace('{page}', $page + 1, $this->url) . '"><i class="icon-arrow-down"></i></a>';
  85. //$output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
  86. }
  87.  
  88. $output .= '</div></div>';
  89.  
  90. if ($num_pages > 1) {
  91. return $output;
  92. } else {
  93. return '';
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment