Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class NewsPagination {
- public $total = 0;
- public $page = 1;
- public $limit = 20;
- public $num_links = 8;
- public $url = '';
- public $text_first = '|<';
- public $text_last = '>|';
- public $text_next = '>';
- public $text_prev = '<';
- public function render() {
- $total = $this->total;
- if ($this->page < 1) {
- $page = 1;
- } else {
- $page = $this->page;
- }
- if (!(int)$this->limit) {
- $limit = 10;
- } else {
- $limit = $this->limit;
- }
- $num_links = $this->num_links;
- $num_pages = ceil($total / $limit);
- $this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
- $output = '<div class="pagination"><!--<a class="btn pagination__btn" href="#">Показать ещё<i class="icon-arrow-down"></i></a>--><div class="pagination__pages">';
- if ($page > 1) {
- //$output .= '<a class="pagination__prev" href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '"><i class="icon-arrow-down"></i></a>';
- if ($page - 1 === 1) {
- $output .= '<a class="pagination__prev" href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '"><i class="icon-arrow-down"></i></a>';
- } else {
- $output .= '<a class="pagination__prev" href="' . str_replace('{page}', $page - 1, $this->url) . '"><i class="icon-arrow-down"></i></a>';
- }
- }
- if ($num_pages > 1) {
- if ($num_pages <= $num_links) {
- $start = 1;
- $end = $num_pages;
- } else {
- $start = $page - floor($num_links / 2);
- $end = $page + floor($num_links / 2);
- if ($start < 1) {
- $end += abs($start) + 1;
- $start = 1;
- }
- if ($end > $num_pages) {
- $start -= ($end - $num_pages);
- $end = $num_pages;
- }
- }
- $output .= '<div class="pagination__pages-list">';
- for ($i = $start; $i <= $end; $i++) {
- if($i == $end){
- $output .= '<div class="pagination__more"><span></span><span></span><span></span></div>';
- }
- if ($page == $i) {
- $output .= '<a class="pagination__pages-item active">' . $i . '</a>';
- } else {
- if ($i === 1) {
- $output .= '<a class="pagination__pages-item" href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '">' . $i . '</a>';
- } else {
- $output .= '<a class="pagination__pages-item" href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a>';
- }
- }
- }
- $output .= '</div>';
- }
- if ($page < $num_pages) {
- $output .= '<a class="pagination__next" href="' . str_replace('{page}', $page + 1, $this->url) . '"><i class="icon-arrow-down"></i></a>';
- //$output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
- }
- $output .= '</div></div>';
- if ($num_pages > 1) {
- return $output;
- } else {
- return '';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment