分页(页码跳转+页码省略)
表格页面
<?php
namespace pdo_edu;
use PDO;
// 1. 连接数据库
require 'conn.php';
//获取第几页
$page = $_GET['page'] ?? 1;
//每页条数
$num = 3;
//总页数
$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `user`";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$pages = $stmt->fetch()['total'];
$sql = "SELECT COUNT(`id`) AS `countid` FROM `user`";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$countid = $stmt->fetch()['countid'];
//echo $pages;
//偏移量
$offset = $num * ($page - 1);
//
$sql = "SELECT * FROM `user` LIMIT {$num} OFFSET {$offset}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll();
//设置分页页码的样式
$headpages = 2;//头和尾各显示几个
$midpages = 3;//中间显示几个(单数)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分页数据展示</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table>
<caption>用户列表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮箱</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) : ?>
<tr>
<th><? echo $user['id'] ?></th>
<th><? echo $user['name'] ?></th>
<th><? echo $user['email'] ?></th>
<th><? echo $user['status'] ?></th>
<th><? echo date('Y年m月d日', $user['create_time']) ?></th>
<th><button>编辑</button><button>删除</button></th>
</tr>
<?php endforeach ?>
</tbody>
</table>
<!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
<p>
<!-- 首页 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=1' ?>">首页</a>
<!-- 前一页 -->
<?php
$prev = $page - 1;
if ($page == 1) $prev = 1;
?>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $prev ?>">前一页</a>
<?php
if($pages<=($headpages*2+$midpages)){
for ($i=1; $i<=$pages; $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
}else{
if($page<=($headpages+ceil($midpages/2))){
for ($i=1; $i<=($headpages+$midpages); $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
printf('<a href="#" class="">...</a>');
for ($i=($pages-$headpages+1); $i<=$pages; $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
}elseif($page>($pages-$headpages-ceil($midpages/2))){
for ($i=1; $i<=($headpages); $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
printf('<a href="#" class="">...</a>');
for ($i=($pages-$headpages-$midpages+1); $i<=$pages; $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
}else{
for ($i=1; $i<=($headpages); $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
printf('<a href="#" class="">...</a>');
for ($i=($page-floor($midpages/2)); $i<=($page+floor($midpages/2)); $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
printf('<a href="#" class="">...</a>');
for ($i=($pages-$headpages+1); $i<=$pages; $i++){
$jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
}
}
}
?>
<!-- 下一页 -->
<?php
$next = $page + 1;
if ($page == $pages) $next = $pages;
?>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $next ?>">下一页</a>
<!-- 尾页 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page='. $pages ?>">尾页</a>
<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" onSubmit="return chk(this)">
<input name="page" type="number" size="3" mix="1" max="<?php echo $pages ?>">
<input type="submit" value="跳转">
</form>
</p>
</body>
</html>
conn.php
<?php
namespace pdo_edu;
use Exception;
use PDO;
$config = [
'type' => $type ?? 'mysql',
'host' => $host ?? 'localhost',
'dbname' => $dbname ?? 'phpedu',
'username' => $username ?? 'root',
'password' => $password ?? 'root',
'charset' => $charset ?? 'utf8',
'port' => $port ?? '3306',
];
$type = $config['type'];
$host = $config['host'];
$dbname = $config['dbname'];
$username = $config['username'];
$password = $config['password'];
$charset = $config['charset'];
$port = $config['port'];
$dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);
try{
$pdo = new PDO($dsn,$username,$password);
}catch (Exception $e){
die($e->getMessage());
}