smarty paging class

<?php
header("Content-Type:text/html;charset=utf8");
// Determine the number of pages
if(isset($_GET["page"])){
	$page=$_GET["page"];
}else{
	$page=1;
}      
//Database connection class
class ConnDB{
	var $ dbtype;
	var $ host;
    var $user;
    var $pwd;
    var $dbname;
   
	//Construction method
    function ConnDB($dbtype,$host,$user,$pwd,$dbname){
		$this->dbtype=$dbtype;
    	$this->host=$host;
		$this->user=$user;
		$this->pwd=$pwd;
		$this->dbname=$dbname;
	}
    / / Implement the connection to the database and return the connection object
    function GetConnId(){
     	
    	if($this->dbtype=="mysql" || $this->dbtype=="mssql"){
    		$dsn="$this->dbtype:host=$this->host;dbname=$this->dbname";
		}else{
			$dsn="$this->dbtype:dbname=$this->dbname";
		}    
		try {
    		$conn = new PDO($dsn, $this->user, $this->pwd); //Initializing a PDO object is to create a database connection object $pdo
			$conn->query("set names utf8");
    		return $conn;
		} catch (PDOException $e) {
    		die ("Error!: " . $e->getMessage() . "<br/>");
		}     	
    }	
}
//Database management class
class AdminDB{		
	function ExecSQL($sqlstr,$conn){		
		$sqltype=strtolower(substr(trim($sqlstr),0,6));
		$rs=$conn->prepare($sqlstr); //Prepare the query statement
		$rs->execute(); //Execute the query statement and return the result set
		if($sqltype=="select"){
			$array=$rs->fetchAll(PDO::FETCH_ASSOC); //Get all the data in the result set
			if(count($array)==0 || $rs==false)
				return false;
			else
				return $array;
		}elseif ($sqltype=="update" || $sqltype=="insert" || $sqltype=="delete"){			
			if($rs)
			    return true;
			else
			    return false;    
		}
	}
}
//Pagination class
class SepPage{
	var $rs;
	var $pagesize;
	var $ nowpage;
	var $array;
	var $conn;
	var $ sqlstr;
	function ShowData($sqlstr,$conn,$pagesize,$nowpage){ //Define method
		if(!isset($nowpage) || $nowpage=="") //Determine whether the variable value is empty
			$this->nowpage=1; //Define the start page of each page
		else
			$this->nowpage=$nowpage;
		$this->pagesize=$pagesize; //Define the number of records output per page
		$this->conn=$conn; //The ID returned by connecting to the database
		$this->sqlstr=$sqlstr; //Executed query statement
		$offset=($this->nowpage-1)*$this->pagesize;
		$sql=$this->sqlstr." limit $offset, $this->pagesize";
		$result=$this->conn->prepare($sql); //Prepare query statement
		$result->execute(); //Execute the query statement and return the result set
		$this->array=$result->fetchAll(PDO::FETCH_ASSOC); //Get all the data in the result set
		if(count($this->array)==0 || $this->array==false)
			return false;
		else
			return $this->array;
	}	
	function ShowPage($contentname,$utits,$anothersearchstr,$anothersearchstrs,$class){
		$str="";
		$res=$this->conn->prepare($this->sqlstr); //Prepare query statement
		$res->execute(); //Execute the query statement and return the result set
		$this->array=$res->fetchAll(PDO::FETCH_ASSOC); //Get all the data in the result set
		$record=count($this->array); //Total count of records

		$pagecount=ceil($record/$this->pagesize); //Calculate how many pages there are
		$str.=$contentname." ".$record." ".$utits." 每页 ".$this->pagesize." ".$utits." 第 ".$this->nowpage." 页/共 ".$pagecount." 页";
		$str.="    ";
		if($this->nowpage!=1)
			$str.="<a href=".$_SERVER['PHP_SELF']."?page=1&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">首页</a>";
		else
			$str.="<font color='#555555'>首页</font>";
		$str.=" ";
		if($this->nowpage!=1)
			$str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->nowpage-1)."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">上一页</a>";
		else
			$str.="<font color='#555555'>Previous page</font>";
		$str.=" ";	
		if($this->nowpage!=$pagecount)
			$str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->nowpage+1)."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">下一页</a>";
		else
			$str.="<font color='#555555'>Next page</font>";	
		$str.=" ";
		if($this->nowpage!=$pagecount)
			$str.="<a href=".$_SERVER['PHP_SELF']."?page=".$pagecount."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">尾页</a>";
		else
			$str.="<font color='#555555'>尾页</font>";
		if(count($this->array)==0 || $this->array==false)			
			return "No data!";
		else
		    return $str;	
	}
}
$connobj=new ConnDB("mysql","localhost","root","","db_pagination");//Database connection class instantiation
$conn=$connobj->GetConnId();//Perform the connection operation and return the connection ID
$admindb=new AdminDB();//Database operation class instantiation
$seppage=new SepPage();//Paging class instantiation
$arraybbs=$seppage->ShowData("select * from tb_mr_book",$conn,1,$page);//Call the paging class to implement the paging function
// $smarty->assign("showpage",$seppage->ShowPage("","","","","")); //Define the template variable showpage for outputting paging data
var_dump($arraybbs);

 Define the paging class using smarty

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324911785&siteId=291194637