php分页显示

<?php
/**
 * Page Class
 * 实现各种分页样式
 * @author yangsh
 */
class Pager {

	/**
	 * 数据总数
	 * @var integer
	 */
	private $totalItems;

	/**
	 * 每页显示数
	 * @var integer
	 */
	private $pageSize = 20;

	/**
	 * 页面显示的页码标号的数量
	 * @var integer
	 */
	private $codeNum = 10;

	/**
	 * 跳转链接
	 * @var string(url)
	 */
	private $jumpLink = '';

	/**
	 * 分页语言
	 * @var string
	 */
	private $parseLang = array();

	/**
	 * 装载页码的[Key]值
	 * @var integer
	 */
	private $keyPage = 'pTag';

	/**
	 * 构造函数
	 * @param $totalItems 总的信息数量
	 * @param $pageSize 每页显示的条数
	 * @param $codeNum 页面显示的页码标号的数量(取偶数,非偶数时取加1)
	 * @param $language 分页显示语言(en,zh,sign)
	 * @param $keyPage 装载分页码标号的变量名称
	 * @param $curtPage 初始页
	 * @param $path 分页页面链接地址
	 */
	public function __construct($totalItems,$pageSize='',$language='',$codeNum='') {
		$this->setTotalItems($totalItems);	//总的数据
		$this->setPageSize($pageSize);		//每页显示数
		$this->setParseLang($language);		//分页语言
		$this->setCodeNum($codeNum);		//页面显示的页码标号的数量
	}

	/**
	 * 装载默认分页HTML
	 * @return string(html)
	 */
	public function parsePage() {
		$str  = $this->getHtmlInfoPage(); //分页信息
		$str .= $this->getHtmlFirstPage(); //第一页
		$str .= $this->getHtmlLastPage(); //上一页
		$str .= $this->getHtmlPageList('',''); //页码
		$str .= $this->getHtmlNextPage(); //下一页
		$str .= $this->getHtmlFinallyPage(); //最后页
		$str .= $this->getHtmlSelectPage(); //下拉列表
		return $str;
	}

	/**
	 * 设置分页语言
	 * @param string $language 语言简写(en|zh|sign)
	 * @return array
	 */
	private function setParseLang($language) {
		$aryLang = array(
			'sign' => array(
			'LAST_PAGE'		=> '<',
			'NEXT_PAGE'		=> '>',
			'FIRST_PAGE'	=> '<<',
			'FINALLY_PAGE'=> '>>',
			'INFO_PAGE'		=> '{CURRENT_PAGE}/{TOTAL_PAGE}',
			'JUMP'			  => 'GO',
			'PAGE'   			=> '',
			'FROM'        => '',
			'BUTTON'      => '',
			),
			'cn'   => array(
			'LAST_PAGE'		=> '上一页',
			'NEXT_PAGE'		=> '下一页',
			'FIRST_PAGE'	=> '首页',
			'FINALLY_PAGE'=> '尾页',
			'INFO_PAGE'		=> '页次:<b>{CURRENT_PAGE}</b>/<b>{TOTAL_PAGE}</b>页 共<b>{TOTALITEM}</b>条记录 ',
			'JUMP'			  => '转到',
			'PAGE'   			=> '页',
			'FROM'        => '第',
			'BUTTON'      => '跳转',
			),
			'en'   => array(
			'LAST_PAGE'		=> 'LAST',
			'NEXT_PAGE'		=> 'NEXT',
			'FIRST_PAGE'	=> 'FIRST',
			'FINALLY_PAGE'=> 'FINAL',
			'INFO_PAGE'		=> 'PAGE {CURRENT_PAGE} OF {TOTAL_PAGE}',
			'JUMP'   			=> 'GO',
			'PAGE'   			=> '',
			'FROM'        => '',
			'BUTTON'      => '',
			),
		);
		if(in_array($language, array_keys($aryLang))) {
			$this->parseLang = $aryLang[$language];
		} else {
			$this->parseLang = $aryLang['cn'];
		}
	}

	/**
	 * 测试用
	 * 得到一些分页相关信息
	 * @return string
	 */
	public function getMessage() {
		$info  = '<center><br><br><table border=1 bordercolor=red><tr><td>';
		$info .= '<font color=blue>';
		$info .= '<center>----------分页类------------</center>';
		$info .= '<b>初始设置:</b><br>共有信息数量 totalItems:' .$this->totalItems;
		$info .= '<br>设置每页显示数量 pageSize:' .$this->pageSize;
		$info .= '<br>设置每页显示的页码数 codeNum:' .$this->codeNum;
		$info .= '<br>设置装载页码的变量名 keyPage:' .$this->keyPage;
		$info .= '<br>设置跳转的URL jumpLink:' .$this->jumpLink;
		$info .= '<br><b>返回信息:</b>';
		$info .= '<br>得到的总页数 getPageNum():' .$this->getPageNum();
		$info .= '<br>得到的当前页码 curtPage:' .$this->getCurtPage();
		$info .= '<br>得到的数据库检索条件 getSqlLimit():' .$this->getSqlLimit();
		$info .= '<br><b>所有样式:</b><br>';
		$info .= 'getHtmlInfoPage(): '.$this->getHtmlInfoPage().'<br>getHtmlFirstPage():'.$this->getHtmlFirstPage().'<br>getHtmlLastPage():'.$this->getHtmlLastPage();
		$info .= '<br>getHtmlPageList(): '.$this->getHtmlPageList('[', ']').'<br>getHtmlNextPage():'.$this->getHtmlNextPage().'<br>getHtmlFinallyPage():'.$this->getHtmlFinallyPage();
		$info .= '<br>getHtmlSelectPage(): '.$this->getHtmlSelectPage();
		$info .= '</font>';
		$info .= '</td></tr></table><br><br></center>';
		print_r($info);
	}

	/**
	 * 设置记录总数
	 * @param int $total
	 * @return void
	 */
	private function setTotalItems($total) {
		$this->totalItems = max(1, intval($total));
	}

	/**
	 * 设置装载页码的[key],为空时表示生成静态页的分页
	 * @param string $str(ex:page)
	 * @return void
	 */
	public function setKeyPage($str) {
		if (!empty($str)) {
			$this->keyPage = $str;
		}
	}

	/**
	 * 设置页面显示的页码标号的数量
	 * @param integer $codeNum
	 */
	private function setCodeNum($codeNum) {
		if (!empty($codeNum)) {
			$this->codeNum = max(2, $codeNum+$codeNum%2); //该值须为偶数
		}
	}

	/**
	 * 设置跳转路径(ex: /path/ex.php[htm])
	 * @param string $fileName 跳转路径(默认跳转至当前页)
	 * @return  void
	 */
	public function setJumpLink($jumpLink) {
		$this->jumpLink = $jumpLink;
	}

	/**
	 * 设置当前页
	 * @param integer $curtPage 当前页码
	 * @return void
	 */
	private function getCurtPage() {
		$curtPage = isset($_GET[$this->keyPage]) ? intval($_GET[$this->keyPage]) : 1;
		$curtPage = max(1, $curtPage);
		$curtPage = min($curtPage, $this->getPageNum());
		return $curtPage;
	}

	/**
	 * 设置每页显示记录数
	 * @param int $pageSize
	 * @return void
	 */
	private function setPageSize($pageSize) {
		if(!empty($pageSize)) {
			$this->pageSize = max(1, intval($pageSize));
		}
	}

	/**
	 * 返回跳转URL
	 * @param int $pageVal 转至的页数
	 * @return string(url)
	 */
	private function getJumpLink($pageVal) {
	  $pageVal = max(0, intval($pageVal));
		if (preg_match('#(\.htm(l?))$#i', $this->jumpLink)) {
			$fary = explode('.', $this->jumpLink);
			$fary[count($fary)-2] .= $pageVal;
			return implode('.', $fary);
		}
		if (!empty($_SERVER['QUERY_STRING'])) {
			$qs = $_SERVER['QUERY_STRING']. '&';
			$qs = preg_replace('#'.$this->keyPage.'=[^\&]*\&?#i', '', $qs);
		} else {
			$qs = '';
		}
		return $this->jumpLink. '?' .$qs.$this->keyPage. '=' .$pageVal;
	}

	/**
	 * 得到总页数
	 * @return integer
	 */
	public function getPageNum() {
		return ceil($this->totalItems / $this->pageSize);
	}

	/**
	 * 得到格式化的分页信息
	 * @return string
	 */
	public function getHtmlInfoPage() {
		$str  = $this->parseLang['INFO_PAGE'];
		$str  = str_replace('{TOTAL_PAGE}', $this->getPageNum(), $str);
		$str  = str_replace('{CURRENT_PAGE}', $this->getCurtPage(), $str);
		$str  = str_replace('{TOTALITEM}' , $this->totalItems, $str);
		return $str;
	}

	/**
	 * 得到格式化的页码标号
	 * @param $trim 为分页标号之间间隔字符,默认为:空格
	 * @return string
	 */
	public function getHtmlPageList($trim_l = ' ',$trim_r = ' ') {
		$str = '';
		//前$codeNum/2页
		for($i=($this->getCurtPage()-($this->codeNum/2)); $i<$this->getCurtPage(); $i++) {
			if ($i < 1) continue;
			$str .= $trim_l. '<a href="' .$this->getJumpLink($i). '">' .$i. '</a>' .$trim_r;
		}
		$str .= '<a class="selected active">' .$trim_l.$i.$trim_r. '</a>';  //当前页
		//后$codeNum/2-1页
		for ($i=($this->getCurtPage()+1); $i<($this->getCurtPage()+($this->codeNum/2)); $i++) {
			if($i > $this->getPageNum()) break;
			$str .= $trim_l. '<a href="' .$this->getJumpLink($i). '">' .$i. '</a>' .$trim_r;
		}
		return $str;
	}

	/**
	 * 得到格式化的第一页
	 * @return string(html)
	 */
	public function getHtmlFirstPage() {
		if($this->getCurtPage() <= 1) {
			 //return ' ' .$this->parseLang['FIRST_PAGE']. ' ';
			 return ' ';
		}
		return ' <a href="' . $this->getJumpLink(1). '">' .$this->parseLang['FIRST_PAGE'] . '</a> ';
	}

	/**
	 * 得到格式化的最后页
	 * @return  string
	 */
	public function getHtmlFinallyPage() {
		if($this->getCurtPage() >= $this->getPageNum()) {
			//return ' ' .$this->parseLang['FINALLY_PAGE']. ' ';
			return ' ';
		}
		return ' <a href="' .$this->getJumpLink($this->getPageNum()). '">' .$this->parseLang['FINALLY_PAGE']. '</a> ';
	}

	/**
	 * 得到格式化后的上一页
	 * @return string
	 */
	public function getHtmlLastPage() {
		if ($this->getCurtPage() <= 1) {
			//return ' ' .$this->parseLang['LAST_PAGE']. ' ';
			return ' ';
		}
		return ' <a href="' .$this->getJumpLink($this->getCurtPage()-1). '">' .$this->parseLang['LAST_PAGE']. '</a> ';
	}

	/**
	 * 得到格式化的下一页
	 * @return string
	 */
	public function getHtmlNextPage() {
		if($this->getCurtPage() >= $this->getPageNum()) {
			//return ' ' .$this->parseLang['NEXT_PAGE']. ' ';
			return ' ';
		}
		return ' <a href="' .$this->getJumpLink($this->getCurtPage()+1). '">' .$this->parseLang['NEXT_PAGE']. '</a> ';
	}

	/**
	 * 得到格式化的以下拉菜单显示
	 * @return string
	 */
	public function getHtmlSelectPage($html='input') {
		$str = $this->parseLang['JUMP']. '<select name="' .$this->keyPage. '" ';
		$str.= 'onChange="javascript:window.location.href=this.options[selectedIndex].value">'.$this->parseLang['PAGE'];
		$s = '';
		for ($i=1; $i<=$this->getPageNum(); $i++) {
			$s = ($i == $this->getCurtPage()) ? ' selected' : '';
			$str .= '<option value="' .$this->getJumpLink($i). '"' .$s.'>' .$i. '</option>';
		}
		$str .= '</select>'.$this->parseLang['PAGE'];

		$str2 = '<label>'.$this->parseLang['FROM'].'<input id="_pTagInput_" type="text" value="'.$this->getCurtPage().'" />';
		$str2.= $this->parseLang['PAGE'].'</label>';
		$str2.= '<label><button onclick="window.location.href=\''.$this->getJumpLink(0).'\'+document.getElementById(\'_pTagInput_\').value">';
		$str2.= $this->parseLang['BUTTON'].'</button></label>';

		return $html == 'select' ? $str : $str2;
	}

	/**
	 * 返回用于外部调用的当前URL
	 */
	public function getArgv() {
		return '&' .$this->keyPage. '=' .$this->getCurtPage();
	}

	/**
	 * 返回SQL语句(数据库查询)
	 * @return  string
	 */
	public function getSqlLimit() {
		return ' LIMIT ' .max(0, intval($this->getCurtPage() * $this->pageSize - $this->pageSize)). ',' .$this->pageSize;
	}

	/**
	 * 返回分页计算参数 start
	 * @return int
	 */
   public function getLimitStart() {
     return max(0, intval($this->getCurtPage() * $this->pageSize - $this->pageSize));
   }

   /**
    * 返回分页计算参数 offset
    * @return int
    */
   public function getLimitOffset() {
     return $this->pageSize;
   }

}
.meneame {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; FONT-SIZE: 80%; PADDING-BOTTOM: 3px; MARGIN: 3px; COLOR: #ff6500; PADDING-TOP: 3px; TEXT-ALIGN: center; clear:both;}
.meneame A {
	BORDER-RIGHT: #ff9600 1px solid; PADDING-RIGHT: 7px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #ff9600 1px solid; PADDING-LEFT: 7px; BACKGROUND-IMAGE: url(meneame.jpg); PADDING-BOTTOM: 5px; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff9600 1px solid; TEXT-DECORATION: none
}
.meneame A:hover {
	BORDER-RIGHT: #ff9600 1px solid; BORDER-TOP: #ff9600 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; BORDER-BOTTOM: #ff9600 1px solid; BACKGROUND-COLOR: #ffc794
}
.meneame A:active {
	BORDER-RIGHT: #ff9600 1px solid; BORDER-TOP: #ff9600 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; BORDER-BOTTOM: #ff9600 1px solid; BACKGROUND-COLOR: #ffc794
}
.meneame SPAN.current,.selected {
	BORDER-RIGHT: #ff6500 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ff6500 1px solid; PADDING-LEFT: 7px; FONT-WEIGHT: bold; PADDING-BOTTOM: 5px; BORDER-LEFT: #ff6500 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff6500 1px solid; BACKGROUND-COLOR: #ffbe94
}
.meneame SPAN.disabled {
	BORDER-RIGHT: #ffe3c6 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ffe3c6 1px solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 5px; BORDER-LEFT: #ffe3c6 1px solid; COLOR: #ffe3c6; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ffe3c6 1px solid
}

posted on 2011-04-22 13:12  天空尚兰  阅读(352)  评论(0编辑  收藏  举报

导航