$recordcount=$rs[0];//共14条记录
$pagesize=1;//每页2条记录
$pagecount=ceil($recordcount/$pagesize);//总共有几页
$currpage=isset($_GET['p']) ? $_GET['p']:1;//显示当前页
<?php include 'inc/db_mysqli.php';?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webrx-php</title>
<?php css1();?> //引用样式
</head>
<body>
<?php
$p = isset($_GET['p']) ? $_GET['p'] : 1;
- //(表明,2表示一页显示几行内容,mname字段名,$p表示当前页)
$rs = pager('member',$p,'mname,maccount',2);
$m->close();
echo $rs[1];
foreach($rs[0] as $v){
echo $v[0].'<br>';
}
echo $rs[1];
?>
</body>
</html>
实现分页的函数
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'db';
$charset = 'utf8';
$m = new mysqli($host,$user,$pass,$dbname);
$m->set_charset($charset);
function pager($tn,$currpage=1,$f='*',$pagesize=3,$w='1=1'){
global $m;
$stmt = $m->prepare("select count(*) from $tn where $w");
$stmt->execute();
$stmt->bind_result($recordcount);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
$stmt = $m->prepare("select $f from $tn where $w limit ?,?");
$pagecount = ceil($recordcount/$pagesize);
$start = $currpage*$pagesize - $pagesize;
$stmt->bind_param('ii',$start,$pagesize);
$stmt->execute();
$result = $stmt->get_result();
$row = array();
$row[] = $result->fetch_all( MYSQLI_NUM);
$stmt->free_result();
$stmt->close();
$first = 1;
$end = 10;
$pages = '<div class="page">';
if($currpage>=7){
$first = $currpage-5;
$end = $first+$end-1;
}
if($currpage>1){
$prev = $currpage-1;
if($first>1){
$pages.="<a href=?p=1>首页</a><a href=?p=$prev>上一页</a>";
}else{
$pages.="<a href=?p=$prev>上一页</a>";
}
}
for($i=$first;$i<=$end;$i++){
if($i>$pagecount){
break;
}
if($i==$currpage){
$pages.='<a class="checked">'.$i.'</a>';
continue;
}
$pages.="<a href=?p=$i>$i</a>";
}
if($currpage<$pagecount){
$next = $currpage+1;
$pages.="<a href=?p=$next>下一页</a>";
}
if($end<$pagecount){
$pages.="<a href=?p=$pagecount>尾页</a>";
}
$row[] = $pages.'</div>';
$row[] = $pagesize;
$row[] = $pagecount;
$row[] = $recordcount;
$row[] = $currpage;
return $row;
}
function css1(){
$css = <<<css
<style>
.page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}
.page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}
.page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}
.page a:visited,.page a:link{color:#333;}
.page a:active{color:#3B3B3B;}
</style>
css;
echo $css;
}