Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- tested with opencart 1.5.4
- */
- class Pagination {
- public $total = 0;
- public $page = 1;
- public $limit = 20;
- public $num_links = 10;
- public $url = '';
- public $text = 'Showing {start} to {end} of {total} ({pages} Pages)';
- public $text_first = '|<';
- public $text_last = '>|';
- public $text_next = '>';
- public $text_prev = '<';
- public $style_links = 'links';
- public $style_results = 'results';
- public $show_next_last = false;
- public $dots_before = '';
- public $dots_after = '';
- public $current_before = '';
- public $current_after = '';
- public $prev_before = '';
- public $prev_after = '';
- public $next_before = '';
- public $next_after = '';
- public $link_before = '';
- public $link_after = '';
- public $output_before = '';
- public $output_after = '';
- 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);
- $output = '';
- if ($page > 1) {
- if ($this->show_next_last) {
- $output .= ' <a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a> ';
- }
- if ($this->prev_before && $this->prev_after) {
- $output .= ' ' . str_replace('{url}', str_replace('{page}', $page - 1, $this->url), $this->prev_before);
- $output .= $this->text_prev;
- $output .= $this->prev_after . ' ';
- } else {
- $output .= ' <a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</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;
- }
- }
- if ($start > 1) {
- $output .= $this->dots_before . ' ... ' . $this->dots_after;
- }
- for ($i = $start; $i <= $end; $i++) {
- if ($page == $i) {
- $output .= $this->current_before;
- $output .= !$this->current_before ? ' <b>' : '';
- $output .= $i;
- $output .= !$this->current_after ? '</b> ' : '';
- $output .= $this->current_after;
- } else {
- if ($this->link_before && $this->link_after) {
- $output .= ' ' . str_replace('{url}', str_replace('{page}', $i, $this->url), $this->link_before);
- $output .= $i;
- $output .= $this->link_after . ' ';
- } else {
- $output .= ' <a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a> ';
- }
- }
- }
- if ($end < $num_pages) {
- $output .= $this->dots_before . ' ... ' . $this->dots_after;
- }
- }
- if ($page < $num_pages) {
- if ($this->next_before && $this->next_after) {
- $output .= ' ' . str_replace('{url}', str_replace('{page}', $page + 1, $this->url), $this->next_before);
- $output .= $this->text_next;
- $output .= $this->next_after . ' ';
- } else {
- $output .= ' <a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> ';
- }
- if ($this->show_next_last) {
- $output .= ' <a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a> ';
- }
- }
- $find = array(
- '{start}',
- '{end}',
- '{total}',
- '{pages}'
- );
- $replace = array(
- ($total) ? (($page - 1) * $limit) + 1 : 0,
- ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
- $total,
- $num_pages
- );
- $return = '';
- if ($output) {
- if ($this->output_before && $this->output_after) {
- $return .= $this->output_before . $output . $this->output_after;
- } else {
- $return .= '<div class="' . $this->style_links . '">' . $output . '</div>';
- }
- }
- if ($this->style_results) {
- $return .= '<div class="' . $this->style_results . '">' . str_replace($find, $replace, $this->text) . '</div>';
- }
- return $return;
- }
- }
- /*
- Usage
- ------
- to get this:
- <div class="pager skew-25">
- <ul>
- <li><a href="#" class="skew25"><i class="fa fa-angle-left"></i></a></li>
- <li class="selected"><span class="skew25">1</span></li>
- <li><a href="#" class="skew25">2</a></li>
- <li><a href="#" class="skew25">3</a></li>
- <li><a href="#" class="skew25">4</a></li>
- <li><a href="#" class="skew25">5</a></li>
- <li><span class="skew25">...</span></li>
- <li><a href="#" class="skew25">10</a></li>
- <li><a href="#" class="skew25"><i class="fa fa-angle-right"></i></a></li>
- </ul>
- </div>
- you would do this:
- // standard opencart code
- $pagination = new Pagination();
- $pagination->total = $review_total;
- $pagination->page = $page;
- $pagination->limit = 5;
- $pagination->text = $this->language->get('text_pagination');
- $pagination->url = $this->url->link('blog/blog/review', 'blog_id=' . $this->request->get['blog_id'] . '&page={page}');
- // new custom code
- $pagination->dots_before = '<li><span class="skew25">';
- $pagination->dots_after = '</span></li>';
- $pagination->current_before = '<li class="selected"><span class="skew25">';
- $pagination->current_after = '</span></li>';
- $pagination->text_prev = '';
- $pagination->text_next = '';
- $pagination->prev_before = '<li><a href="{url}" class="skew25"><i class="fa fa-angle-left">';
- $pagination->prev_after = '</i></a></li>';
- $pagination->next_before = '<li><a href="{url}" class="skew25"><i class="fa fa-angle-right">';
- $pagination->next_after = '</i></a></li>';
- $pagination->link_before = '<li><a href="{url}" class="skew25">';
- $pagination->link_after = '</a></li>';
- $pagination->output_before = '<div class="pager skew-25"><ul>';
- $pagination->output_after = '</ul></div>';
- $pagination->style_results = '';
- $pagination->num_links = 7;
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement