<?php
class page{
private $dbadd; //數(shù)據(jù)庫地址
private $dbuser; //數(shù)據(jù)庫用戶名
private $dbpwd; //數(shù)據(jù)庫密碼
private $dbname; //數(shù)據(jù)庫名
private $tablename; //數(shù)據(jù)表名
private $num; //每頁顯示的行數(shù)
private $total; //表中數(shù)據(jù)總數(shù)
private $totalpage; //總頁數(shù)
private $page; //當(dāng)前頁數(shù)
private $limit; //查詢數(shù)
private $arrs; //查詢后返回的數(shù)據(jù)
private $col; //總列數(shù)
private $nostart; //當(dāng)前頁開始的記錄數(shù)
private $noend; //當(dāng)前頁結(jié)束的記錄數(shù)
private $head; //首頁
private $end; //尾頁
private $last; //上一頁
private $nexts; //下一頁
function __construct($dbadd, $dbuser, $dbpwd, $dbname, $tablename, $num){
$this->dbadd=$dbadd;
$this->dbuser=$dbuser;
$this->dbpwd=$dbpwd;
$this->dbname=$dbname;
$this->tablename=$tablename;
$this->num=$num;
$this->total=$this->gettotal(); //得到總條數(shù)
$this->totalpage=ceil($this->total/$this->num); //獲取總頁數(shù)
$this->page=$this->getpage(); //得到當(dāng)前頁數(shù)
$this->limit=$this->setlimit(); //得到查詢的數(shù)據(jù)
$this->arrs=$this->getarrs(); //得到查詢的結(jié)果
$this->col=$this->getcol(); //獲取總列數(shù)
$this->nostart=($this->page-1)*$num+1;
$this->noend=$this->page==$this->totalpage ? $this->total : $this->page*$num;
$this->head=$this->page==1 ? "首頁" : "<a href='?page=1'>首頁</a>";
$this->end=$this->page==$this->totalpage ? "尾頁" : "<a href='?page={$this->totalpage}'>尾頁</a>";
$this->last=$this->page==1 ? "上一頁" : "<a href='?page=".($this->page-1)."'>上一頁</a>";
$this->nexts=$this->page==$this->totalpage ? "下一頁" : "<a href='?page=".($this->page+1)."'>下一頁</a>";
}
private function getcol(){
$linkss=mysql_connect($this->dbadd, $this->dbuser, $this->dbpwd);
mysql_select_db($this->dbname, $linkss);
$result=mysql_query("select * from {$this->tablename}", $linkss);
$col=mysql_num_fields($result);
mysql_close($linkss);
return $col;
}
private function getarrs(){ //根據(jù)條件再次查詢并返回結(jié)果
$sql="select * from {$this->tablename} limit {$this->limit}, {$this->num}";
$links=mysql_connect($this->dbadd, $this->dbuser, $this->dbpwd);
mysql_select_db($this->dbname, $links);
$result=mysql_query($sql, $links);
while($arr=mysql_fetch_assoc($result)){
$arrs[]=$arr;
}
return $arrs;
mysql_close($links);
}
private function setlimit(){
return ($this->page-1)*$this->num;
}
private function getpage(){
$page=isset($_GET["page"]) ? $_GET["page"] : 1;
$page=$page>$this->totalpage ? $this->totalpage : $page;
$page=$page<1 ? 1 : $page;
return $page;
}
private function gettotal(){ //得到表中所有記錄的總數(shù)
$link=mysql_connect($this->dbadd, $this->dbuser, $this->dbpwd);
mysql_select_db($this->dbname, $link);
$result=mysql_query("select * from {$this->tablename}", $link);
$total=mysql_num_rows($result);
mysql_close($link);
return $total;
}
function __get($args){
switch($args){
case "arrs": //根據(jù)條件查詢出的結(jié)果,是二維數(shù)組
return $this->arrs;
break;
case "totalpage": //總頁數(shù)
return $this->totalpage;
break;
case "col": //總列數(shù)
return $this->col;
break;
case "page": //當(dāng)前頁
return $this->page;
break;
case "nostart": //當(dāng)前頁顯示的記錄的起始條數(shù)
return $this->nostart;
break;
case "noend": //當(dāng)前頁顯示的記錄的結(jié)束條數(shù)
return $this->noend;
break;
case "head": //首頁按鈕
return $this->head;
break;
case "end": //尾頁按鈕
return $this->end;
break;
case "last": //上一頁按鈕
return $this->last;
break;
case "nexts": //下一頁按鈕
return $this->nexts;
break;
}
}
}
?>
這個類沒有公有的方法,像上一頁、下一頁、首頁、末頁、當(dāng)前頁數(shù)、總頁數(shù)、當(dāng)前顯示條數(shù)、總條數(shù)這些值,分別賦給了成員屬性,調(diào)用時只需要輸出相應(yīng)的成員屬性即可,非常方便。
在實例化傳值的時候,只需要傳數(shù)據(jù)庫地址、帳號、密碼、庫名、表名、每頁顯示的數(shù)目即可!
這個類很好用 故在這里唐山長城網(wǎng)絡(luò)給大家給大家推薦一下
http://