PHP分页技术通用模版

来源:互联网 发布:起点数据查询 编辑:程序博客网 时间:2024/05/01 22:01
直接进入主题吧!可能不是最好的,但相信自己是自己做的会是最棒的,^_^

首先建一个domain:fenyePage.class.php

<?php
class fenyePage{
public $everyPageRows; //每页显示的行数
public $sumPage;       //总页数
public $nowPage;       //当前所在页数
public $fenyeArray;    //分页显示的数据
public $navigate;      //分页导航条
public $pageWhole;     //翻页页数
function showNavigate(){
echo "<ulclass='fenye_ul'>";
echo"<li>共{$this->sumPage}页</li>";
echo "<ahref='userList.php?nowPage=1'><li>首页</li></a>";
if($this->nowPage>1){echo"<ahref='userList.php?nowPage=".($this->nowPage-1)."'><liclass='btn'>上页</li></a>";}
//翻页
$startPage =floor(($this->nowPage-1)/$this->pageWhole)* $this->pageWhole + 1;
$index = $startPage;
//如果当前页是在1到10之间,就没有必要显示向前翻页的链接
if($this->nowPage >$this->pageWhole){echo "<ahref='userList.php?nowPage=".($startPage-1)."'><li><b>&lt;&lt;</b></li></a>";}
for(;$startPage<$index +$this->pageWhole;$startPage++){
if($startPage == $this->nowPage){
echo "<ahref='userList.php?nowPage=$startPage'><listyle='background:#6699cc;'>$startPage</li></a>";
}else{
echo "<ahref='userList.php?nowPage=$startPage'><li>$startPage</li></a>";
}
}
//如果startPage的值小于总的页数,就显示向后翻译
if($startPage<$this->sumPage){echo"<ahref='userList.php?nowPage=$startPage'><li><b>&gt;&gt;</b></li></a>";}
if($this->nowPage<$this->sumPage){echo"<ahref='userList.php?nowPage=".($this->nowPage+1)."'><li>下页</li></a>";}
echo "<ahref='userList.php?nowPage={$this->sumPage}'><li>末页</li></a>";
echo "</ul>";
}
}
?>


接着就是分页导航条的一些css样式:global.css
@charset "utf-8";

body{
font-family:Arial, Helvetica, sans-serif ;
font-size:12px;
}

a:link{
text-decoration:none;
color:#333333;
}

a:hover{
text-decoration: none;
color: #006699;
}

a:visited{
text-decoration: none;
color: #008040;
}


.fenye_ul{
list-style-type:none;
padding:0;
float:right;
}
.fenye_ul li{
float:left;
border:1px solid #6699cc;
text-align:center;
margin-left:3px;
padding:2px 5px;
font-size:10px;
}
.fenye_ul .btn{
float:left;
border:1px solid #6699cc;
text-align:center;
margin-left:2px;
font-size:10px;
}

再接着,就是在db.class.php这个工具类中,新建一个分页使用的方法:
//分页查询的方法
function fenyeSelect($sql_arrs,$sql_sumPage,$fenyePage){
//获取分页显示的数据
$res = mysql_query($sql_arrs,$this->conn) ordie(mysql_errno());
$array = array();
while($row = mysql_fetch_row($res)){
$array[] = $row;
}
//释放资源
mysql_free_result($res);
//获取分页所需要的显示数据
$fenyePage->fenyeArray = $array;
//获取总的数据行数
$res2 = mysql_query($sql_sumPage,$this->conn)or die(mysql_errno());
if($rows = mysql_fetch_row($res2)){
//获取总的页数
$fenyePage->sumPage =ceil($rows[0]/$fenyePage->everyPageRows);
}
//释放资源
mysql_free_result($res2);
}

继续,就是在service文件夹中新建对应的一个文件:userService.php
<?php
 
require_once '../dao/db.class.php';
class userService{
//获取分页显示数据方法
function getFenYePage($fenyePage){
$dbClass = new dbClass();
$sql_arrs = "selectuser_name,user_gender,user_professional,user_email,user_addr,user_phonefrom user_info order by id limit ".
($fenyePage->nowPage-1)*$fenyePage->everyPageRows.",".$fenyePage->everyPageRows;
$sql_sumPage = "select count(id) from user_info";

$dbClass->fenyeSelect($sql_arrs,$sql_sumPage,$fenyePage);
$dbClass->connClose();
}
}
?>


最后,就是页面层的文件:userList.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"content="text/html; charset=utf-8" />
<title>通讯录管理系统</title>
<link rel="stylesheet" type="text/css"href="css/global.css">
</head>
<body>
<p>友友管理 &gt;友友信息</p>
<hr />
<table width="725" border="1" cellspacing="0"cellpadding="0" style="border-collapse:collapse"bordercolor="#6699CC">
  <trbgcolor="#6699cc">
    <thscope="col">姓名</th>
    <thscope="col">性别</th>
    <thscope="col">专业</th>
    <thscope="col">Email</th>
    <thscope="col">籍贯</th>
    <thscope="col">电话</th>
    <thscope="col">操&nbsp;作</th>
  </tr>
 <?php 
  require_once'../domain/fenyePage.class.php';
  require_once'../service/userService.php';
 
  $fenyePage = new fenyePage();
 
  //如果当前页未获取到,则默认为首页
  $fenyePage->nowPage =1;
  if(!empty($_GET['nowPage'])){
  $fenyePage->nowPage =$_GET['nowPage'];
  }
  //设置默认每页显示几条数据
  $fenyePage->everyPageRows =10;
  //默认翻页页数
 $fenyePage->pageWhole=10;
  //
$userServie = new userService();
$userServie->getFenYePage($fenyePage); 
for($i=0;$i<count($fenyePage->fenyeArray);$i++){
  $row =$fenyePage->fenyeArray[$i];
echo "<tr align='center'height='25px'>";
echo"<td>$row[0]</td>";
echo"<td>$row[1]</td>";
echo"<td>$row[2]</td>";
echo"<td>$row[3]</td>";
echo"<td>$row[4]</td>";
echo"<td>$row[5]</td>";
echo "<td><ahref='#'>修改</a>&nbsp;||&nbsp;<ahref='#'>删除</a></td>";
echo "</tr>";
  }
  ?>
</table><?php$fenyePage->showNavigate();?>
</body>
</html>


原创粉丝点击