<?php
class page{
private $dbadd; //數(shù)據(jù)庫(kù)地址
private $dbuser; //數(shù)據(jù)庫(kù)用戶(hù)名
private $dbpwd; //數(shù)據(jù)庫(kù)密碼
private $dbname; //數(shù)據(jù)庫(kù)名
private $tablename; //數(shù)據(jù)表名
private $num; //每頁(yè)顯示的行數(shù)
private $total; //表中數(shù)據(jù)總數(shù)
private $totalpage; //總頁(yè)數(shù)
private $page; //當(dāng)前頁(yè)數(shù)
private $limit; //查詢(xún)數(shù)
private $arrs; //查詢(xún)后返回的數(shù)據(jù)
private $col; //總列數(shù)
private $nostart; //當(dāng)前頁(yè)開(kāi)始的記錄數(shù)
private $noend; //當(dāng)前頁(yè)結(jié)束的記錄數(shù)
private $head; //首頁(yè)
private $end; //尾頁(yè)
private $last; //上一頁(yè)
private $nexts; //下一頁(yè)
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); //獲取總頁(yè)數(shù)
$this->page=$this->getpage(); //得到當(dāng)前頁(yè)數(shù)
$this->limit=$this->setlimit(); //得到查詢(xún)的數(shù)據(jù)
$this->arrs=$this->getarrs(); //得到查詢(xún)的結(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 ? "首頁(yè)" : "<a href='?page=1'>首頁(yè)</a>";
$this->end=$this->page==$this->totalpage ? "尾頁(yè)" : "<a href='?page={$this->totalpage}'>尾頁(yè)</a>";
$this->last=$this->page==1 ? "上一頁(yè)" : "<a href='?page=".($this->page-1)."'>上一頁(yè)</a>";
$this->nexts=$this->page==$this->totalpage ? "下一頁(yè)" : "<a href='?page=".($this->page+1)."'>下一頁(yè)</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ù)條件再次查詢(xún)并返回結(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ù)條件查詢(xún)出的結(jié)果,是二維數(shù)組
return $this->arrs;
break;
case "totalpage": //總頁(yè)數(shù)
return $this->totalpage;
break;
case "col": //總列數(shù)
return $this->col;
break;
case "page": //當(dāng)前頁(yè)
return $this->page;
break;
case "nostart": //當(dāng)前頁(yè)顯示的記錄的起始條數(shù)
return $this->nostart;
break;
case "noend": //當(dāng)前頁(yè)顯示的記錄的結(jié)束條數(shù)
return $this->noend;
break;
case "head": //首頁(yè)按鈕
return $this->head;
break;
case "end": //尾頁(yè)按鈕
return $this->end;
break;
case "last": //上一頁(yè)按鈕
return $this->last;
break;
case "nexts": //下一頁(yè)按鈕
return $this->nexts;
break;
}
}
}
?>
這個(gè)類(lèi)沒(méi)有公有的方法,像上一頁(yè)、下一頁(yè)、首頁(yè)、末頁(yè)、當(dāng)前頁(yè)數(shù)、總頁(yè)數(shù)、當(dāng)前顯示條數(shù)、總條數(shù)這些值,分別賦給了成員屬性,調(diào)用時(shí)只需要輸出相應(yīng)的成員屬性即可,非常方便。
在實(shí)例化傳值的時(shí)候,只需要傳數(shù)據(jù)庫(kù)地址、帳號(hào)、密碼、庫(kù)名、表名、每頁(yè)顯示的數(shù)目即可!
這個(gè)類(lèi)很好用 故在這里唐山長(zhǎng)城網(wǎng)絡(luò)給大家給大家推薦一下
http://