日期:2014-05-17  浏览次数:20561 次

php通用分页类
<?php
interface ILink
{
	public function parse($page,$param);
}
?>

?

<?php
require'ILink.php';
class LinkAdapter implements ILink
{
	/**
	 * @param unknown_type $page
	 * @param unknown_type $param
	 */
	public function parse($page, $param) 
	{
		
		$temp="共{$page->getAllPage()}页,第{$page->getCurrentPage()}页&nbsp;";
		$links=$this->getLinkString($param);
		if($page->hasPrevious())$temp.="<a href='$links=".($page->getCurrentPage()-1)."'>上一页</a>&nbsp;";else{$temp.="上一页&nbsp;";}
		for($i=$page->getCurrentPage();$i<=$page->getAllPage()&&$i<=$page->getPerRecords();$i++)
		{
			$temp.="<a href='$links=$i'>{$i}</a>&nbsp;";
		}
		if($page->hasNext())$temp.="<a href='$links=".($page->getCurrentPage()+1)."'>下一页</a>&nbsp;";else{$temp.="下一页&nbsp;";}
		return $temp;
	}
	public function getLinkString($param)
	{
		$str="";
		$attr=$_GET;
		unset($attr[$param]);
		if($attr)
		{
			foreach($attr as $key=>$val)
			{
				if($str=="")
				{
					$str.="?$key=$val";
				}
				else
				{
					$str.="&$key=$val";
				}
			}
			$str.="&$param";
		}
		else
		{
			$str.="?$param";
		}
		return $str;
	}
}
?>

??

<?php
class Page 
{
	private $allPage;#总页数
	private $allRecords;#总记录数
	private $perRecords;#单页记录数
	private $currentPage=1;#当前页面
	/**
	 * @return the $allPage
	 */
	public function getAllPage()
	{
		return $this->allPage;
	}
	/**
	 * @return the $allRecords
	 */
	public function getAllRecords() 
	{
		return $this->allRecords;
	}

	/**
	 * @return the $perRecords
	 */
	public function getPerRecords() {
		return $this->perRecords;
	}

	/**
	 * @return the $currentPage
	 */
	public function getCurrentPage() 
	{
		return $this->currentPage;
	}

	/**
	 * @param $allPage the $allPage to set
	 */
	public function setAllPage($allPage) 
	{
		$this->allPage = ($allPage%$this->perRecords == 0)?($allPage/$this->perRecords):($allPage/$this->perRecords+1);
		$this->allPage=intval($this->allPage);
	}

	/**
	 * @param $allRecords the $allRecords to set
	 */
	public function setAllRecords($allRecords) 
	{
		$this->allRecords = $allRecords;
	}

	/**
	 * @param $perRecords the $perRecords to set
	 */
	public function setPerRecords($perRecords) {
		$this->perRecords = $perRecords;
	}

	/**
	 * @param $currentPage the $currentPage to set
	 */
	public function setCurrentPage($currentPage) 
	{
		if ($currentPage < 1)
			$this->currentPage = 1;
		else if ($currentPage > $this->allPage)
			$this->currentPage =$this->allPage;
		else
			$this->currentPage=$currentPage;
	}
	public function hasNext() 
	{
		return $this->currentPage<$this->allPage;
	}
	public function hasPrevious() 
	{
		return $this->currentPage>1;
	}
	public function getEndIndex() 
	{
		return ((($this->currentPage-1)*$this->perRecords)+$this->perRecords)>$this->allRecords?((($this->currentPage-1)*$this->perRecords)+$this->perRecords)-$this->allRecords:$this->perRecords;
	}
	public function getStartIndex() 
	{
		return ($this->currentPage-1)*$this->perRecords;
	}
}
?>

?

<?php
require'Page.php';
require'LinkAdapter.php';
class Pager 
{
	private $list=array();
	private $page;#分页对象
	private $param;#页面请求参数
	public function __construct($list)
	{
		$this->list=$list;
		$this->page=new Page();
	}
	/**
	 * 
	 * @param unknown_type $rows 显示的数据量
	 * @param unknown_type $current 当前页
	 */
	public function init($rows=5,$current)
	{
		$this->page->setAllRecords(count($this->list));
		$this->page->setPerRecords($rows);
		$this->page->setAllPage(count($this->list));
		$this->page->setCurrentPage($current);
		
		$this->list=array_slice($this->list,$this->page->getStart