一、分页类设计Page.php
<?php
class Page {
public $pageSize=6;//每页显示记录数
public $rowCount;//记录总数数
public $pageNow=1; //当前页
public $pageCount=1; //总页数
public $navigate; //导航
public $url; //地址
public $firstRow; //每页显示的第一条记录
public $rollPage; //分栏每页显示页数
public $startNum; //【1】
public $endNum;
public function setNav(){
$navigate="";
//本导航的起始页
if($this->startNum>1){
$jump=$this->startNum-$this->rollPage;
//$nav=$this->url."/pageNow/$jump";
$navigate.= "<a href='{$this->url}?pageNow=$jump'><<</a> ";
}
for($start=$this->startNum;$start<=$this->endNum;$start++)
{
$navigate.="<a href='{$this->url}?pageNow=$start'>[{$start}]</a>";
}
if($this->endNum<$this->pageCount)
{
$jump=$this->startNum+$this->rollPage;
$navigate.= "<a href='{$this->url}?pageNow=$jump'>>></a> ";
}
$navigate.="第".$this->pageNow."页/共".$this->pageCount."页";
$this->navigate=$navigate;
}
public function setPage($url,$pageNow,$rollPage=4,$pageSize=6,){
$this->url=$url;
$this->pageNow=$pageNow;
$this->pageSize=$pageSize;
$this->rollPage=$rollPage;
}
public function setNavigate(){
$this->pageCount=ceil($this->rowCount/($this->pageSize+0.0));
$this->firstRow=($this->pageNow-1)*$this->pageSize;
$this->startNum=floor(($this->pageNow-1)/$this->rollPage)*$this->rollPage+1;
$this->endNum=$this->startNum+$this->rollPage-1;
if($this->endNum>$this->pageCount)
{
$this->endNum=$this->pageCount;
}
if($this->rowCount==0){
$this->pageNow=1;
$this->rollPage=1;
$this->firstRow=1;
$this->pageCount=1;
}
$this->setNav(); //字符串存储导航
}
}
?>
二、SqlHelper.class.php定义分页显示方法
public function excute_dql_page($sql1,$sql2,&$page){
//数据表信息分页
//sql1数据,sql2求行数
$result=mysql_query($sql1,$this->conn)or die(mysql_errno());
$arr=array();
while($row=mysql_fetch_assoc($result)){
$arr[]=$row;
}
mysql_free_result($result);
$result=mysql_query($sql2,$this->conn) or die(mysql_errno());
if($row=mysql_fetch_row($result)){
$page->rowCount=$row[0];
//实现导航条
$page->setNavigate();
}
mysql_free_result($result);
//数组存储记录集
return $arr;
}
三、UserService.class.php调用分页
<?php
require_once 'SQLHelper.class.php';
require_once 'User.class.php';
class UserService{
public function getUserListByPage(&$page){
$sqlHelper=new SQLHelper();
$startNum=($page->pageNow-1)*$page->pageSize;
$sql1="select id,name from users limit $startNum,$page->pageSize";
$sql2="select count(*) from users";
$res=$sqlHelper->excute_dql_page($sql1, $sql2, $page);
$sqlHelper->close_connect();
}
}
四、使用分页(userList.php)
<?php
header("content-type:text/html;charset=utf-8");
require_once 'SQLHelper.class.php';
require_once 'UserService.class.php';
echo"用户信息表<br/>";
echo"<table border=1 width=600px>
<tr><th>id</th><th>name</th><th>删除</th><th>修改</th></tr>";
//创建page并设置属性值
$page=new Page();
$page->url="userList.php";
$page->pageNow=1;
if(isset($_GET['pageNow'])){
$page->pageNow=$_GET['pageNow'];
}
//创建Service负责产生一个记录集(分页后的)
$page->setPage($url,$pageNow);
$userService=new UserService();
$userService->getUserListByPage($fenyePage);
foreach ($page->res_array as $row){
$id=$row['id'];
echo"<tr><td>{$row['id']}</td><td>{$row['name']}</td><td><a href='userProcess.php?action=delete&id=$id'>删除</a></td><td><a href='updateUserUI.php?id=$id'>修改</a></td></tr>";
//print_r($row['id']);
}
echo"</table>";
echo $fenyePage->navigate;
?>
<form action="userList.php" method="get">
到第 <input type="text" size="1" name="pageNow" />页<input type="submit" value="确定"/>
</form>