已知页数求当前行数:
p1 limit 0,3
p2 limit 3,3
p3 limit 6,3
p4 limit 9,3
页数和当前页条数的算法:
offset=(p-1)*3;
index.php
<?php
include 'config.php';
// 每页条数
$length=3;
// 总页数
$sqltot="select count(*) from user";
$smtot=$pdo->prepare($sqltot);
$smtot->execute();
$tot=$smtot->fetchColumn();
$totPage=ceil($tot/$length);
//当前页
$page=$_GET['p'];
if($page>=$totPage){
$page=$totPage;
}
// offset
$offset=($page-1)*$length;
// 下一页
$nextPage=$page+1;
if($nextPage>=$totPage){
$nextPage=$totPage;
}
// 上一页
$prePage=$page-1;
if($prePage<=1){
$prePage=1;
}
// 求对应页面数据
$sql="select * from user limit {$offset},{$length}";
$smt=$pdo->prepare($sql);
$smt->execute();
$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户模块</title>
<link rel="stylesheet" href="/bs/css/bootstrap.css">
<script src='/bs/js/jquery.js'></script>
<script src='/bs/js/bootstrap.js'></script>
<style>
td,th{
text-align: center;
vertical-align:middle!important;
}
.nowPage {
padding: 0px;
text-align: center;
width: 30px;
}
.col-md-2 input{
margin-left: 10px;
float: left;
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<h1 class="page-header">
用户管理
</h1>
<table class='table table-striped table-hover table-bordered'>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>修改</th>
<th>删除</th>
</tr>
<?php
foreach($rows as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td><a href='edit.php?id={$row['id']}' class='btn btn-warning'>修改</a></td>";
echo "<td><a href='delete.php?id={$row['id']}' class='btn btn-danger'>删除</a></td>";
echo "</tr>";
}
?>
</table>
<div class="row">
<div class="col-md-4">
<a href="index.php?p=1" class="btn btn-primary">首页</a>
<a href="index.php?p=<?php echo $prePage ?>" class="btn btn-primary">上一页</a>
<a href="index.php?p=<?php echo $nextPage ?>" class="btn btn-primary">下一页</a>
<a href="index.php?p=<?php echo $totPage ?>" class="btn btn-primary">末页</a>
<button class="btn btn-default">总计:<?php echo $totPage ?>页</button>
</div>
<div class="col-md-2">
<form action="index.php">
<input type="text" name="p" class="form-control nowPage" value="<?php echo $page ?>">
<input type="submit" value="go" class="btn btn-success">
</form>
</div>
</div>
</div>
</body>
</html>
**config.php
<?php
$servername = "localhost";
$username = "root";
$password = "123";
$dbname='myweb';
$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->exec('set names utf8');
?>
PDO分页类
index.php
<?php
class Page{
public $offset;
public $length;
public $tot;
public $prePage;
public $nextPage;
public $totPage;
public $page;
public function __construct($tot,$length){
$this->tot=$tot;
$this->length=$length;
// 总页数
$totPage=ceil($tot/$length);
$this->totPage=$totPage;
//当前页
$page=$_GET['p'];
if($page>=$totPage){
$page=$totPage;
}
$this->page=$page;
// 下一页
$nextPage=$page+1;
if($nextPage>=$totPage){
$nextPage=$totPage;
}
$this->nextPage=$nextPage;
// 上一页
$prePage=$page-1;
if($prePage<=1){
$prePage=1;
}
$this->prePage=$prePage;
// offset
$offset=($page-1)*$length;
$this->offset=$offset;
}
}
?>
Page.class.php
<?php
include 'config.php';
include 'Page.class.php';
// 总页数
$sqltot="select count(*) from user";
$smtot=$pdo->prepare($sqltot);
$smtot->execute();
$tot=$smtot->fetchColumn();
$page=new Page($tot,3);
// 求对应页面数据
$sql="select * from user limit {$page->offset},{$page->length}";
$smt=$pdo->prepare($sql);
$smt->execute();
$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户模块</title>
<link rel="stylesheet" href="/bs/css/bootstrap.css">
<script src='/bs/js/jquery.js'></script>
<script src='/bs/js/bootstrap.js'></script>
<style>
td,th{
text-align: center;
vertical-align:middle!important;
}
.nowPage {
padding: 0px;
text-align: center;
width: 30px;
}
.col-md-2 input{
margin-left: 10px;
float: left;
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<h1 class="page-header">
用户管理
</h1>
<table class='table table-striped table-hover table-bordered'>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>修改</th>
<th>删除</th>
</tr>
<?php
foreach($rows as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td><a href='edit.php?id={$row['id']}' class='btn btn-warning'>修改</a></td>";
echo "<td><a href='delete.php?id={$row['id']}' class='btn btn-danger'>删除</a></td>";
echo "</tr>";
}
?>
</table>
<div class="row">
<div class="col-md-4">
<a href="index.php?p=1" class="btn btn-primary">首页</a>
<a href="index.php?p=<?php echo $page->prePage ?>" class="btn btn-primary">上一页</a>
<a href="index.php?p=<?php echo $page->nextPage ?>" class="btn btn-primary">下一页</a>
<a href="index.php?p=<?php echo $page->totPage ?>" class="btn btn-primary">末页</a>
<button class="btn btn-default">总计:<?php echo $page->totPage ?>页</button>
</div>
<div class="col-md-2">
<form action="index.php">
<input type="text" name="p" class="form-control nowPage" value="<?php echo $page->page ?>">
<input type="submit" value="go" class="btn btn-success">
</form>
</div>
</div>
</div>
</body>
</html>
config.php
<?php
$servername = "localhost";
$username = "root";
$password = "123";
$dbname='myweb';
$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->exec('set names utf8');
?>
**
**
**
**
**
**
**
**