批改状态:合格
老师批语:分页功能里面有许多小技巧的
try{// 连接数据库$pdo = new PDO('mysql:host=localhost;dbname=tp5;charset=utf8','root','root');}catch(PDOException $e){echo $e->getMessage();}catch(Throwable $e){echo $e->getMessage();}
查询分页数据文件 dataGet.php
<?php// 引进数据库连接require 'connect.php';// 获取分页数据// 1.每页显示的数量$num = 5;// 2.当前页码,默认为1$page = $_GET['p'] ?? 1;// 3.计算每一页的第一条记录的显示偏移量$offset = ($page-1) * $num;// 4.获取分页数据$sql = "SELECT * FROM `tp_user` LIMIT {$num} OFFSET {$offset}";$users = $pdo->query($sql)->fetchAll();print_r($users);echo '<hr>';// 计算总页数// 计算表中共有多少条记录// 每一页显示几条// 总页数 = ceil(记录总数 / 每页的记录数)$sql1 = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `tp_user`";// 计算总页数$pages = $pdo->query($sql1)->fetch()['total'];echo $pages;

数据显示
先引进查询数据文件
<?php require 'dataGet.php' ?>
把查询到的数据渲染在table表格中
<!-- 表格 --><table><caption>用户信息表</caption><thead><tr><td>id</td><td>name</td><td>age</td><td>操作</td></tr></thead><tbody><?php foreach($users as $user): ?><tr><td><?=$user['id']?></td><td><?=$user['username']?></td><td><?=$user['age']?></td><td><button>编辑</button><button>删除</button></td></tr><?php endforeach ?></tbody></table>

添加分页栏
<!-- 分页栏 --><!-- 分页条的动态生成 --><!-- 跳转地址的动态生成 --><!-- 当前页码的高亮显示 --><?php for($i=1;$i<=$pages;$i++): ?><?php$jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);// echo $jump;// $i: 分页中的页码// $page: 在URL中通过GET获取的页码?p=x$active = ($i == $page) ? 'active' : null;?><a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a><?php endfor ?>


分页功能完善
添加上一页以及下一页等分界
<p><!-- 上一页:解决页面的越界 --><?php// 获取前一页的页码$prev = $page - 1;if($page == 1) $prev = 1;?><?php if($page!=1):?><a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首页</a><a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一页</a><?php endif ?><!-- 正常界面数 --><?php for($i=1;$i<=$pages;$i++): ?><?php$jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);$active = ($i == $page) ? 'active' : null;?><a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a><?php endfor ?><!-- 下一页:解决页面的越界 --><?php// 获取下一页的页码$next = $page + 1;if($next>=$pages) $next = $pages;?><?php if($page!=$pages): ?><a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一页</a><a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾页</a><?php endif ?></p>



编辑用户信息
编辑操作
// 获取操作$action = $_GET['action'];$id = $_GET['id'];switch($action){// 1:渲染编辑菜单case 'edit':// 加载,渲染数据编辑菜单include 'edit.php';break;// 2.执行编辑操作case 'doedit':// 更新$sql = "UPDATE `tp_user` SET `username`=?,`age`=? WHERE `id`=?";$stmt = $pdo->prepare($sql);// 新的数据在$_POSTif(!empty($_POST)){$stmt->execute([$_POST['username'],$_POST['age'],$id]);if($stmt->rowCount() == 1){echo '<script>alert("更新成功");location.href="demo2.php"</script>';}}break;}
编辑页面
<?php// 获取要被编辑的数据$user = $pdo->query("SELECT * FROM `tp_user` WHERE `id`={$id}")->fetch();print_r($user);?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>用户编辑</title></head><body><h3>编辑用户</h3><form action="<?echo $_SERVER['PHP_SELF']. '?action=doedit&id='.$id?>" method="post"><p><label for="username">用户名:</label><input type="text" name="username" id="username" value="<?=$user['username']?>"></p><p><label for="age">年龄:</label><input type="age" name="age" id="age" value="<?=$user['age']?>"></p><p><button>保存</button></p></form></body></html>
编辑跳转
<tbody><?php foreach($users as $user): ?><tr><td><?=$user['id']?></td><td><?=$user['username']?></td><td><?=$user['age']?></td><td><button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">编辑</button><button>删除</button></td></tr><?php endforeach ?></tbody>
编辑前
编辑中

编辑成功后
删除用户信息
删除入口
<button onclick="location.href='handle.php?action=del&id=<?=$user['id']?>'">删除</button>
删除操作
// 3.执行删除操作case 'del':// 删除$sql = "DELETE FROM `tp_user` WHERE `id`=?";$stmt = $pdo->prepare($sql);$stmt->execute([$id]);if($stmt->rowCount() == 1) echo '<script>alert("删除成功");location.href="demo2.php"</script>';break;
删除前
删除中
删除成功后
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号