PHP分頁技術

來源:互聯網
上載者:User
1.分頁對象

View Code

 1 

2.分頁查詢

View Code

 1 conn=mysql_connect($this->host,$this->username,$this->password,$this->dbname);16             if(!$this->conn){17                 die(mysql_error());18             }19             //選擇資料庫20             mysql_select_db($this->dbname,$this->conn);21             //設定編碼22             mysql_query("set names utf8");23         }24         25         //執行dql語句26         public function execute_dql($sql){27             $res=mysql_query($sql,$this->conn);28             return $res;29         }30         31         //執行dml語句32         public function execute_dml($sql){33             $b=mysql_query($sql,$this->conn);34             if(!$b){35                 //執行失敗36                 return 0;37             }38             else{39                 if(mysql_affected_rows($this->conn)>0){40                     //執行成功41                     return 1;42                 }43                 else{44                     //沒有受影響的行45                     return 2;46                 }47             }48         }49         50         //分頁查詢51         public function execute_dql_pagging($sql1,$sql2,$paggingModel){52             //擷取分頁所需要的資料集53             $res=mysql_query($sql1,$this->conn) or die(mysql_errno());54             $arr=array();55             //將結果集儲存起來56             while($row=mysql_fetch_assoc($res)){57                 $arr[]=$row;58             }59             //釋放結果集60             mysql_free_result($res);61             62             //擷取記錄總數63             $res=mysql_query($sql2,$this->conn) or die(mysql_errno());64             if($row=mysql_fetch_row($res)){65                 $paggingModel->pageCount=ceil($row[0]/$paggingModel->pageSize);66                 $paggingModel->rowCount=$row[0];67             }68             69             mysql_free_result($res);70             71             //儲存結果集72             $paggingModel->res_array=$arr;73         }74         75         //關閉串連76         public function close_connect(){77             if(!empty($this->conn)){78                 mysql_close($this->conn);79             }80         }81     }82 ?>

3.分頁邏輯

View Code

 1 pageNow-1)*$paggingModel->pageSize.",".$paggingModel->pageSize;10             $sql2="select count(*) from table";11             $sqlHelper->execute_dql_pagging($sql1, $sql2, $paggingModel);12             13             $sqlHelper->close_connect();14         }15     }16 ?>

4.測試分頁

View Code

 1 pageNow=1; 8     $paggingModel->pageSize=6; 9     10     //改變當前頁11     if(!empty($_GET['pageNow'])){12         $paggingModel->pageNow=$_GET['pageNow'];13     }14     15     $paggingBuiness=new PaggingBuiness();16     $paggingBuiness->getPagging($paggingModel);17     18     //列印表格結構以及表頭19     echo "html代碼";20     21     for($i=0;$ires_array);$i++){22         $row=$paggingModel->res_array[$i];23         echo "輸出內容";24     }25     26     //顯示上一頁和下一頁27     if($paggingModel->pageNow>1){28         $prePage=$paggingModel->pageNow-1;29         echo "a href='xx.php?pageNow=$prePage'>上一頁 ";30     }31     if($paggingModel->pageNow<$paggingModel->pageCount){32         $nextPage=$paggingModel->pageNow+1;33         echo "下一頁 ";34     }35 ?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.