龚哥哥 爱生活、做自己!
PHP生成分页类 前端基于amazeui
发表于 2015-8-31 | PHP
<?php

/**
 * 分页驱动
 * @author  Devil
 * @version v_1.0.0
 */
class PageLibrary
{
	private $page;
	private $total;
	private $number;
	private $bt_number;
	private $where;
	private $page_total;
	private $url;
	private $html;

	/**
	 * [__construct description]
	 * @param [int]    $param['total'] 		[数据总数]
	 * @param [int]    $param['number'] 	[每页数据条数]
	 * @param [int]    $param['bt_number'] 	[分页显示按钮个数]
	 * @param [array]  $param['where'] 		[额外条件(键值对)]
	 * @param [string] $param['url'] 		[url地址]
	 */
	public function __construct($param = array())
	{
		$this->page = max(1, isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
		$this->total = max(1, isset($param['total']) ? intval($param['total']) : 1);
		$this->number = max(1, isset($param['number']) ? intval($param['number']) : 1);
		$this->bt_number = isset($param['bt_number']) ? intval($param['bt_number']) : 2;
		$this->where = (isset($param['where']) && is_array($param['where'])) ? $param['where'] : '';
		$this->url = isset($param['url']) ? $param['url'] : '';
		$this->page_total = 1;
		$this->html = '';

		/* 参数设置 */
		$this->SetParem();
	}

	/**
	 * [SetParem 参数设置]
	 */
	private function SetParem()
	{
		/* 防止超出最大页码数 */
		$this->page_total = ceil($this->total/$this->number);
		if($this->page > $this->page_total) $this->page = $this->page_total;

		/* 额外条件url设置 */
		if(!empty($this->where) && is_array($this->where))
		{
			foreach($this->where as $k=>$v) $this->url .= '&'.$k.'='.$v;
		}
		
	}

	/**
	 * [GetPageHtml 获取生成好的分页代码]
	 */
	public function GetPageHtml()
	{
		$this->html .= '<ul class="am-pagination am-pagination-centered"><li';
		$this->html .= ($this->page > 1) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page-1).'" class="meila-radius">&laquo;</a></li>';
		$this->html .= $this->GetButtonNumberHtml();
		$this->html .= '<li';
		$this->html .= ($this->page > 0 && $this->page < $this->page_total) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page+1).'" class="meila-radius">&raquo;</a></li></ul>';

		return $this->html;
	}

	/**
	 * [GetButtonNumberHtml 获取button显示个数的html]
	 * @return [string] [按钮个数html代码]
	 */
	private function GetButtonNumberHtml()
	{
		$html_before = '';
		$html_after = '';
		$html_page = '<li class="am-active"><a class="meila-radius">'.$this->page.'</a></li>';
		if($this->bt_number > 0)
		{
			/* 前按钮 */
			if($this->page > 1)
			{
				$total = ($this->page-$this->bt_number < 1) ? 1 : $this->page-$this->bt_number;
				for($i=$this->page-1; $i>=$total; $i--)
				{
					$html_before = '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>'.$html_before;
				}
			}

			/* 后按钮 */
			if($this->page_total > $this->page)
			{
				$total = ($this->page+$this->bt_number > $this->page_total) ? $this->page_total : $this->page+$this->bt_number;
				for($i=$this->page+1; $i<=$total; $i++)
				{
					$html_after .= '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>';
				}
			}
		}
		return $html_before.$html_page.$html_after;
	}

	/**
	 * [GetPageStarNumber 获取分页起始值]
	 */
	public function GetPageStarNumber()
	{
		return intval(($this->page-1)*$this->number);
	}
}
?>

发表评论:

TOP