PHP分页技术

WBOY
WBOYOriginal
2016-06-23 14:30:17802browse

1.分页对象

View Code

 1 <?php 2     //保存分页信息的类 3     class PaggingMode{ 4         //分页规格 5         public $pageSize=6; 6         //结果集 7         public $res_array; 8         //总记录 9         public $rowCount;10         //当前页11         public $pageNow;12         //总页数13         public $pageCount;14         //分页导航15         public $navigate;16     }17 ?>

2.分页查询

View Code

 1 <?php 2     require_once 'PaggingModel.php'; 3     //MySql助手类(含分页) 4     class SqlHelper{ 5      6         //属性 7         public $conn; 8         public $dbname="database"; 9         public $username="root";10         public $password="root";11         public $host="localhost";12 13         //构造函数14         public function __construct(){15             $this->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 <?php 2      3     require_once 'SqlHelper.php'; 4     require_once 'PaggingModel.php'; 5     //分页逻辑 6     class PaggingBuiness{ 7         function getPagging($paggingModel){ 8             $sqlHelper=new SqlHelper(); 9             $sql1="select * from table limit ".($paggingModel->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 <?php 2     //测试分页 3     require_once 'PaggingBuiness.php'; 4     require_once 'PaggingModel.php'; 5      6     $paggingModel=new PaggingMode(); 7     $paggingModel->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;$i<count($paggingModel->res_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'>上一页</a> ";30     }31     if($paggingModel->pageNow<$paggingModel->pageCount){32         $nextPage=$paggingModel->pageNow+1;33         echo "<a href='xx.php?pageNow=$nextPage'>下一页</a> ";34     }35 ?>

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php面试题2Next article:php面试题1