php网站中的做好的分页技术-分页实例

db.php中的代码:(//为注释内容)

<?php
//连接数据库开始,数据库名为:ren,用户名:root,密码为空。
$dsn = 'mysql:host=localhost;dbname=ren';
$user = 'root';
$pass = '';
$opt = array(1002=>'set names utf8');
try{
$m = new pdo($dsn,$user,$pass,$opt);
}catch(PDOExeception $e){
exit('数据库连接失败');
}


//分页的css样式开始
$css=<<<css
<style>
.pager {font-size:12px;height:20px;text-align: center; color:#005bac; margin:0 auto; font-family:微软雅黑;}   
.pager a {text-decoration:none; border-radius:5px; border:1px solid #ccc; margin: 0 2px; padding:3px 8px 3px 8px; color: #005bac;}   
.pager a:hover {border:1px solid #aaa;border-radius:5px; color:#f05bac; }   
.pager span.current {border-radius:5px; font-weight: bold;  padding:3px 8px 3px 8px; color: #111; background-color:#005bac; color:#fff}
</style>
css;
//分页的css样式结束


//定义分页函数
function page($t,$p,$pagesize=5,$f='*',$w='1=1',$order=''){
global $m;


$c = "select count(*) from $t where $w";
$recordcount = $m->query($c)->fetchColumn();//求出总记录数

$i = 'ok';
if($recordcount>0){


$pagecount = ceil($recordcount/$pagesize);  //算出总页数


$p = $p<1 ? 1 : $p;                     //如果当前页小于1,则为1


$p = $p>$pagecount ? $pagecount : $p;   //如果当前页大于总页数,则为总页数


$start = $p*$pagesize - $pagesize;      
$sql = "select * from $t where $w $order limit $start,$pagesize";
$rs = $m->query($sql)->fetchAll(PDO::FETCH_NUM);
$ss = 1;
  if($p>=7) $ss = $p-5; 
   $ee = $ss + 10;


   if($ee>$pagecount && $pagecount>10) $ss = $pagecount-10;//在数据很多的情况下,保证每个页面的分页号的个数都为11个。
$page = '<div class="pager">';


  if($ss>1) $page.="<a href='?p=1'>首页</a>";             //输出首页按钮


  if($p>1) $page.=sprintf("<a href='?p=%d'>上一页</a>",$p-1);  //输出下一页按钮


  for($i=$ss;$i<=$ee;$i++){

if($i>$pagecount) break;
if($i==$p){

$page.=sprintf("<span class='current'>%d</span>",$i);  //控制当前页号的样式
continue;
}
       $page.=sprintf("<a href='?p=%d'>%d</a>",$i,$i);
}


   if($p<$pagecount) $page.=sprintf("<a href='?p=%d'>下一页</a>",$p+1);    //输出下一页按钮
   if($i<=$pagecount) $page.=sprintf("<a href='?p=%d'>尾页(%d)</a>",$pagecount,$pagecount);  //输出尾页按钮


}else{
return 0;
}
return array($rs,$page,$p,$pagesize,$pagecount,$recordcount);
}




fenye.php中的代码:(//,<!---->为注释内容)



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>分页显示数据</title>
<style>
tr.data:hover{background:yellow;}
</style>
</head>


<body>
<table bgcolor="gray" width="900" border="0" align="center" cellpadding="10" cellspacing="1">
    <tr bgcolor="#e0e0e0">
      <th width="74" scope="col">编号</th>
      <th width="154" scope="col">姓名</th>
      <th width="67" scope="col">成绩</th>
    </tr>
    <?php
    include 'inc/db.php';     //引入db.php文件

$p = isset($_GET['p']) ? $_GET['p'] : 1;   //判断p是否存在值,存在就是他本身,不存在则为1

$myp = page('job',$p,3,'*','1=1','order by id desc');   //调用db.php中的分页函数

//判断是否查询出数据,如果没有查询到,则返回无数据,否则正常输出
if($myp===0){
echo "<tr bgcolor='#FaFaFa' align='center'><td colspan='5'>无数据</td></tr>";
}else{

echo "<tr bgcolor='#FaFaFa'><td colspan='5'>$css $myp[1]</td></tr>";   //输出css样式,以及分页

$c = '#eeeeee';      //定义颜色,下边是设置隔行颜色
foreach($myp[0] as $v){
$c = $c=='#eeeeee' ? '#FFFFFF' : '#eeeeee';
?>
<tr class="data" bgcolor="<?=$c?>">
        
        <!--------输出从数据库中查询的信息-------->
 <td><?=$v[0]?></td>
 <td><?=$v[1]?></td>
 <td><?=$v[2]?></td>


</tr>
<?php } ?>
        
        <!-----输出分页---------->
<tr bgcolor="#FaFaFa"><td colspan="3"><?=$myp[1]?></td></tr>
    <?php
}
unset($m);
?>
</table>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值