- <?php
- /*
- * 直接輸出數據表和分頁信息
- * 在新建對象時需要的變量:$query(從數據表中獲取記錄數的sql語句),$page(當前頁碼),$maxline(每頁幾行))
- * 1、showpage方法:如果上面創建對象的$query正確,直接調用,即可輸出分頁信息
- * 2、showtable方法:需要的變量:$query(從數據庫讀取記錄的SQL語句,不要加Limit,因為在方法中已經添加)
- * 直接輸出<tr><td></td></tr>的表格,所以只需在前后加上<table></table>就是完整的表格
- * 3、showresult方法:根據提交的$query中的SQL,直接將$result資源返回,表格可以自己定義
- * 示例:
- //獲取當前頁,并定義每頁最大行
- $page=1;
- $maxline="10";
- if(!empty($_GET["page"])){
- $page=$_GET["page"];
- }
- //定義計算表內數據總數的SQL語句,這里必須和下面的$query是同一個表和條件,創建對象,輸出頁碼和表格
- $query="select count(*) from mailbox";
- $a=new PageList($query, $maxline, $page);
- $a->showpage();
- //這里顯示列表,需要和上面的SQL語句一樣的條件
- $query="select username,name,quota,created,modified,active from mailbox order by created desc";
- echo "<table width='800' border='0' cellspacing='0' cellpadding='0' class='pagelist'>";
- $a->showtable($query);
- echo "</table>";
- * */
- class PageList{
- private $link;
- private $result;
- private $maxline;
- private $page=1;
- private $startline=0;
- private $countline;
- public $countpage;
- private $prevpage;
- private $nextpage;
- //數據庫聯接,需要修改為您自己的地址
- private $dbhost=DBHOST;
- private $dbuser=DBUSER;
- private $dbpasswd=DBPASSWD;
- private $dbtable=DBTABLE;
- /*
- * 構造函數中建立數據庫聯接
- * 1、數據庫連接的4個參數設置為常量 記錄在config.php頁面中
- * 2、連接數據庫,并選擇數據庫
- * 3、設置數據庫執行的編碼方式為utf8
- * 4、將接收到的$maxline,$page兩個變量賦值給類屬性,成為該類通用屬性
- * (其中$query是count(*)的SQL,和下面方法中的query是不一樣的)
- * 5、根據新建對象時遞交的$query語句,對數據庫執行查詢,將得到的總記錄數賦值到類屬性中$this->countline
- * 將總記錄數/每頁行數,再用ceil函數高位取整,得到總頁數并賦值到類屬性中$this->countpage
- * 6、根據遞交的當前頁碼$page,算出前后頁的數字$this->prevpage和$this->nextpage
- * 還有必須算出數據庫讀取的起始行$this->startline
- * 這里分3種情況,page<2 ,page=最后一頁,page>1(這個情況可以不判斷,直接用else)
- * */
- public function __construct($query,$maxline,$page){
- @$this->link=mysql_connect($dbhost,$dbuser,$dbpasswd) or die($this->feedback='System Error ,Please contect admin');
- @mysql_select_db($dbtable,$this->link) or die($this->feedback='System Error ,Please contect admin');
- @mysql_query('set names utf8');
- $this->maxline=$maxline;
- //讀取行數,并將結果返回$coutline
- $this->result=mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
- if($count=mysql_fetch_row($this->result)){
- //intval將字符串轉為int,可以不轉,但這樣的程序更健康
- $this->countline = intval($count[0]);
- $this->countpage = ceil($this->countline/$maxline);
- }
- //判斷遞交的$page是否大于總頁數
- if($page<=$this->countpage){
- $this->page=$page;
- }
- if($this->page<2){
- $this->prevpage=0;
- $this->nextpage=2;
- $this->startline= 0;
- }elseif($this->page==$this->countpage){
- $this->prevpage=$this->page-1;
- $this->nextpage=0;
- $this->startline= ($this->page-1)*$this->maxline;
- }else{
- $this->prevpage=$this->page-1;
- $this->nextpage=$this->page+1;
- $this->startline= ($this->page-1)*$this->maxline;
- }
- }
- /*
- * 析構函數
- * 釋放資源,關閉數據庫連接
- * */
- public function __destruct(){
- mysql_free_result($this->result);
- mysql_close($this->link);
- exit();
- }
- /*
- * 輸出分頁信息
- * */
- public function showpage(){
- //$listnum顯示上下頁中間的數字位數,一定要偶數阿!否則不能被2除
- $listnum=10;
- echo $this->countline." Items, ".$this->countpage." Pages ";
- if($this->prevpage==0){
- echo "<<Prev ";
- }else{
- echo "<a href=?page=".$this->prevpage."><<Prev</a> ";
- }
- if($this->countpage<$listnum){ //判斷總頁數是否小于$listnum
- $page_start=1;
- $page_end=$this->countpage;
- }elseif($this->page<$listnum/2){ //判斷當前頁是否小于$listnum的一半
- $page_start=1;
- $page_end=$listnum;
- }elseif($this->page>$this->countpage-($listnum/2)){ //判斷當前頁是否是最后幾頁了
- $page_start=$this->countpage-($listnum-1);
- $page_end=$this->countpage;
- }else{ //如果上面的條件都不符合,那當前也正在中間
- $page_start=$this->page-($listnum/2-1);
- $page_end=$this->page+($listnum/2);
- }
- for($i=$page_start;$i<=$page_end;$i++){ //根據上面判斷的start和end頁碼,循環輸出之間的頁碼
- if($i==$this->page){
- echo "<b>".$i."</b> ";
- }else{
- echo "<a href=?page=".$i.">".$i."</a> ";
- }
- }
- if ($this->nextpage==0){
- echo " Next>>";
- }else{
- echo " <a href=?page=".$this->nextpage.">Next>></a> ";
- }
- }
- /*
- * 根據sql語句讀取數據庫中的數據,然后列成表單輸出
- * 需要的變量:$field(字段),$table(表名),$startline(開始行),$maxline(每頁顯示行數)
- * 輸出從表格的tr開始,從tr結束,所以在使用這個方法前后要加table的標簽
- * */
- public function showtable($query){
- $query=$query." LIMIT ".$this->startline.",".$this->maxline;
- $result = mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
- //行循環開始,定義一個$i變量,用來顯示行號,每次執行一條while語句,指針就指向下一條數據
- $i=0;
- while ($fetch = mysql_fetch_assoc($result)){
- $i++;
- echo "<tr><td>".$i."</td>";
- //列循環開始,因為通過while后,$fetch已經是個數組,所以通過foreach遍歷數組即可
- foreach ($fetch as $value){
- echo "<td>".$value."</td>";
- }
- echo "</tr>";
- }
- }
- /*
- * 這個方法是將資源傳出,表格在外面自定義樣式
- * */
- public function showresult($query){
- $result = mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
- return $result;
- }
- }
- ?>
新聞熱點
疑難解答