【PHP高級】分頁類

<?php
include("mysql/config.inc.php");
class pageClass{
	private $pageIndex;//當前頁碼
	private $pageSize;//每頁顯示記錄數
	private $count;//總記錄數
	private $pageCount;//總頁數
	private $condition;
	private $order;//排序
	private $mysql;//選擇的數據庫操作類
	private $tableName;//需要分頁的表名
	
	//當前頁數屬性
	function setPageIndex($value){
		$this->pageIndex=$value;	
	}
	function getPageIndex(){
		return $this->pageIndex;
	}
	
	//每頁顯示記錄數屬性
    function setPageSize($value){
		$this->pageSize=$value;
		//改變每頁顯示的記錄數會影響總頁數
         $this->getPageCount();
	}
	function getPageSize(){
		return $this->pageSize;
	}
	
	//總記錄數屬性
	function getCount(){
    $this->InnerGetCount();//調用自己的內部函數  
		return $this->count;
	}
	//總頁數屬性
	function getPageCount(){//總頁數等於總記錄數除以每頁顯示記錄數
	  $this->InnerGetCount();//調用自己的內部函數  
		return $this->pageCount=ceil(($this->count/$this->pageSize));
		if($this->pageCount==0){
			$this->pageCount=1;
			return $this->pageCount;
				
								}
		
							}					
	//查詢條件
	function setCondition($value){
		$this->condition=$value;
		//設置條件會影響總記錄數和總頁數
       $this->getPageCount();
	}
	//排序
    function setOrder($value){
		$this->order=$value;
	}	
	//選擇數據庫操作類
	function setMysql(){
		$this->mysql;
	}
	
	//構造函數初始化配置
	function __construct($tableName){
		$this->tableName=$tableName;
		$this->pageIndex=1;
		$this->pageSize=1;
		$this->pageCount=1;
		$this->count=0;
		$this->condition='';
		$this->order='';
		
		//數據庫操作類對象
			if(DBTYPE=='mysql'){
			include_once("mysql/mysqlhelper.php");
			$this->mysql=new mysqlhelper(DB);
								}
			elseif(DBTYPE=='mysqli'){
			include_once("mysql/MysqliHelper.php");
			$this->mysql=new mysqlihelper(DB);
		      					}
		
			}
	//獲取分頁記錄
	function getRecord(){
		$sql="select * from $this->tableName where 1=1 $this->condition $this->order limit ".($this->pageIndex-1)*$this->pageSize.",".$this->pageSize."";
		return $this->mysql->Execute($sql);  //一定要有return 返回值
	}
	
	//獲取總記錄數
private function innerGetCount(){
	$sql="select count(*) from $this->tableName where 1=1 $this->condition";	
	$result=$this->mysql->Execute($sql,2);
	$this->count=$result[0][0];
	}
	
}

?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章