【opencart3源码分析】分页类pagination.php

<?php
/**
 * @package		OpenCart
 * @author		Daniel Kerr
 * @copyright	Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
 * @license		https://opensource.org/licenses/GPL-3.0
 * @link		https://www.opencart.com
*/

/**
* 分页类
*/
class Pagination {
	public $total = 0;
	// 页数
	public $page = 1;
	// 每页限制条数
	public $limit = 20;
	// 链接数
	public $num_links = 8;
	// url
	public $url = '';
	// 首页
	public $text_first = '|&lt;';
	// 尾页
	public $text_last = '&gt;|';
	// 上一页
	public $text_next = '&gt;';
	// 下一页
	public $text_prev = '&lt;';

	/**
     *
     * 分发
     * @return	text
     */
	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);
        // url
		$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
        //输出
		$output = '<ul class="pagination">';

		if ($page > 1) {
			$output .= '<li class="page-item"><a href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '" class="page-link">' . $this->text_first . '</a></li>';

			if ($page - 1 === 1) {
				$output .= '<li class="page-item"><a href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '" class="page-link">' . $this->text_prev . '</a></li>';
			} else {
				$output .= '<li class="page-item"><a href="' . str_replace('{page}', $page - 1, $this->url) . '" class="page-link">' . $this->text_prev . '</a></li>';
			}
		}

		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;
				}
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
				} else {
					if ($i === 1) {
						$output .= '<li class="page-item"><a href="' . str_replace(array('&amp;page={page}', '?page={page}', '&page={page}'), '', $this->url) . '" class="page-link">' . $i . '</a></li>';
					} else {
						$output .= '<li class="page-item"><a href="' . str_replace('{page}', $i, $this->url) . '" class="page-link">' . $i . '</a></li>';
					}
				}
			}
		}

		if ($page < $num_pages) {
			$output .= '<li class="page-item"><a href="' . str_replace('{page}', $page + 1, $this->url) . '" class="page-link">' . $this->text_next . '</a></li>';
			$output .= '<li class="page-item"><a href="' . str_replace('{page}', $num_pages, $this->url) . '" class="page-link">' . $this->text_last . '</a></li>';
		}

		$output .= '</ul>';

		if ($num_pages > 1) {
			return $output;
		} else {
			return '';
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/81416147
今日推荐