当前位置:Gxlcms > PHP教程 > PHP分页技术

PHP分页技术

时间:2021-07-01 10:21:17 帮助过:18人阅读

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 ?>

人气教程排行