<?php
/**
 * 分页类
 */
	Class PageFgwh
	{
		protected $currentPage;//当前页
		protected $pageRecords;//每页记录数
		protected $totalRecords;//总共多少记录数
		protected $totalPages;//共几页	
		protected $args;//除页码外的参数
		
		/**
		 * 构造函数
		 * @param $page 当前页面
		 * @param $total 总记录数
		 * @param $view_records 每页显视记录数
		 * @param $args 其它GET参数
		 * @return null
		 */
		public function __construct($page=1,$total=0,$view_records=10,$args='')
		{
			if(intval($page)){
				$this -> currentPage = $page;
			}else{
				$this -> currentPage = 1;
			}
			$this -> totalRecords = $total;
			$this -> pageRecords = $view_records;
			$this -> args = $args;
			if(intval($total) && intval($view_records)){
				$this -> totalPages = ceil($total / $view_records);
			}
		}
		
		/**
		 * 获取SQL限制字符串
		 * @return string
		 */
		public function getLimitString(){
			$limitString='';
			$records = $this -> pageRecords;
			$curpage = $this -> currentPage;
			$num = $this -> totalRecords;			
			if(intval($records) && intval($curpage) && intval($curpage) && $curpage<=$this -> totalPages){
				$startRecord = ($curpage-1)*$records;
				$endRecord = $curpage*$records;
				$limitString = " limit ".$startRecord.",".$records;
			}else{
				$limitString = " limit 0,".$records;
			}
			return $limitString;
		}
		
		/**
		 * 分页数据
		 * @return Array
		 * @author yhy
		 */
		public function getPageArray(){
			$pageArray['totalnum'] = $this -> totalRecords;//总记录数
			$argsstr = empty($this -> args)?'':"&".$this -> args;
			if($this -> currentPage == 1){//上一页
				$pageArray['pageup'] = '?page=1'.$argsstr;
			}else{
				$pageArray['pageup'] = '?page='.(intval($this -> currentPage) - 1).$argsstr;
			}
			if($this -> currentPage == $this -> totalPages)//下一页
			{
				$pageArray['pagedown'] = '?page='.$this -> currentPage .$argsstr;
			}else{
				$pageArray['pagedown'] = '?page='.($this -> currentPage + 1).$argsstr;
			}
			$pageArray['firstpage'] = '?page=1'.$argsstr;//首页
			$pageArray['lastpage'] = '?page='.$this -> totalPages.$argsstr;//尾页
			$pageArray['totalpages'] = $this -> totalPages;//总页数
			
			if(($this -> currentPage) > ($this ->totalPages) ){
				$pageArray['currentpage'] = 1;//当前页
			}else{
				$pageArray['currentpage'] = $this -> currentPage;//当前页
			}
			if($this -> totalRecords != 0){
				return $pageArray;
			}else{
				$pageArray['pageup'] = "#";
				$pageArray['pagedown'] = "#";
				$pageArray['firstpage'] = "#";
				$pageArray['lastpage'] = "#";
				$pageArray['currentPage'] = 0;
				$pageArray['totalPages'] = 0;
				return $pageArray;
			}
		}
	}
?>