PHP:PDO分页技术、PDO分页类

已知页数求当前行数:

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');

?>

**


**


**



**


**


**



**


**


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值