代码块
demo1.php
<?php
$pdo = new PDO('mysql:host=localhost;dbname=video','root','root',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]);
//获取分页
//当前页码
$page = $_GET['p']??1;
//每页显示的条数
$num = 1;
//偏移量
$offset = ($page-1)*$num;
//获取分页数据
$sql = "select `id`,`username`,`truename` from `admins` order by `id` asc limit {$num} offset {$offset}";
$users = $pdo->query($sql)->fetchAll();
//print_r($users);
//获取总页数
$sql = "select ceil(count(`id`)/{$num}) as `total` from `admins`";
$pages = $pdo->query($sql)->fetch()['total'];
demo2.php
<?php
require 'demo1.php';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="paging/style.css">
</head>
<body>
<table>
<caption>用户信息表</caption>
<thead><tr>
<td>编号</td>
<td>用户名</td>
<td>真实姓名</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user):?>
<tr>
<td><?=$user['id']?></td>
<td><?=$user['username']?></td>
<td><?=$user['truename']?></td>
<td><button>修改</button><button>删除</button></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php
//省略点分页
//分页条显示的页数
$showPage = 3;
//分页条的开始码页
$startPage = 1;
//分页条的结束码页
$endPage = $pages;
//分页条的终止页码相对于当前页码的偏移量
$offset = ($showPage-1)/2;
if ($showPage<$pages) {
if ($page > $offset+1) {
$startOmit = '...';
$startPage = $page-$offset;
$endPage = $page+$offset;
if($endPage>$pages) $endPage=$pages;
} else {
$startPage = 1;
$endPage = $showPage;
}
if ($showPage<$pages && $page+$offseta<$pages) {
$endOmit = '...';
}
}
?>
<!--动态生成分页-->
<p>
<!-- 首页 上一页 下一页 尾页-->
<?php $prev = $page-1;if ($page==1) $prev==1;if ($page!=1): ?>
<a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首页</a>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一页</a>
<?endif;?>
<?php if (isset($startOmit)):?>
<a href="#"><?=$startOmit?></a>
<?endif;?>
<?php
for ($i=$startPage; $i<=$endPage; $i++){
$jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i==$page) ? 'active' : null;
?>
<a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
<? }?>
<?php if (isset($endOmit)):?>
<a href="#"><?=$endOmit?></a>
<?endif;?>
<!-- 下一页-->
<?php $next=$page+1; if ($page==$pages) $next=$page;if ($page!=$pages):?>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一页</a>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾页</a>
<?endif;?>
</p>
</body>
</html>
效果
composer自动加载机制:了解
第三方验证码库:熟练使用