批改状态:合格
老师批语:先别考虑代码的精简, 学习阶段, 实现功能第一, 做对比什么都重要, 等有了一定的经验后, 不用教 ,你自己都明白哪里应该简化的
<?php$pdo = new PDO("mysql:host=localhost;dbname=user;charset=utf8", "root", "root");// var_dump($pdo);// 获取每页显示数量$num = 5;// 获取页码$page = $_GET['p'] ?? 1;// 获取偏移量$offset = ($page - 1) * $num;$sql = "SELECT * FROM `apple` LIMIT {$num} OFFSET {$offset}";$users = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);// 计算总页数$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `apple`";$pages = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC)['total'];?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>分页</title><style>p {text-align: center;}table {border-collapse: collapse;width: 300px;text-align: center;margin: auto;font-size: 18px;}tr:first-of-type {background-color: lightskyblue;}p:last-of-type {display: flex;justify-content: center;}a {text-decoration: none;text-align: center;/* border: 0.5px solid black; */padding: 0 5px;margin-left: 3px;color: lightslategrey;font-size: small;}.active {background-color: lightsalmon;border: lightsalmon;color: white;}</style></head><body><p>用户信息</p><table border=1><tr><th>id</th><th>姓名</th><th>性别</th><th>操作</th></tr><?php foreach ($users as $user) : ?><tr><td><?= $user['id'] ?></td><td><?= $user['username'] ?></td><td><?= $user['sex'] ?></td><td><button onclick="location.href='hendle.php?action=action&id=<?= $user['id'] ?>&p=<?= $page ?>'">编辑</button><button onclick="location.href='hendle.php?action=del&id=<?= $user['id'] ?>&p=<?= $page ?>'">删除</button></td></tr><?php endforeach ?></table><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 if ($page <= 3) : ?><?php for ($i = 1; $i <= 5; $i++) : ?><?php// 动态生成跳转地址$jump = $_SERVER['PHP_SELF'] . '?p=' . $i;// 页码高亮的动态生成$active = ($i == $page) ? 'active' : null;?><a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a><?php endfor ?><a href="">...</a><a href="<?= $_SERVER['PHP_SELF'] . '?p=' . ($pages - 1) ?>" class="<?= $active ?>"><?= $pages - 1 ?></a><a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $pages ?>" class="<?= $active ?>"><?= $pages ?></a><!-- 分页中间部分 --><?php elseif ($page >= 4 && $page < $pages - 4) : ?><a href="<?= $_SERVER['PHP_SELF'] . '?p=1' ?>" class="<?= $active ?>">1</a><a href="<?= $_SERVER['PHP_SELF'] . '?p=2' ?>" class="<?= $active ?>">2</a><a href="">...</a><?php for ($i = $page - 1; $i <= $page + 1; $i++) : ?><?php// 动态生成跳转地址$jump = $_SERVER['PHP_SELF'] . '?p=' . $i;// 页码高亮的动态生成$active = ($i == $page) ? 'active' : null;?><a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a><?php endfor ?><a href="">...</a><a href="<?= $_SERVER['PHP_SELF'] . '?p=' . ($pages - 1) ?>" class="<?= $active ?>"><?= $pages - 1 ?></a><a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $pages ?>" class="<?= $active ?>"><?= $pages ?></a><!-- 分页后半部分 --><?php else : ?><a href="<?= $_SERVER['PHP_SELF'] . '?p=1' ?>" class="<?= $active ?>">1</a><a href="<?= $_SERVER['PHP_SELF'] . '?p=2' ?>" class="<?= $active ?>">2</a><a href="">...</a><?php for ($i = $pages - 4; $i <= $pages; $i++) : ?><?php// 动态生成跳转地址$jump = $_SERVER['PHP_SELF'] . '?p=' . $i;// 页码高亮的动态生成$active = ($i == $page) ? 'active' : null;?><a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a><?php endfor ?><?php endif ?><!-- 下一页 --><?php// 防止下一页越界$next = $page + 1;if ($next > $pages) $next = $pages;?><!-- 判断是否要显示下一页和尾页 --><?php if ($page != $pages) : ?><a href="<?= $_SERVER['PHP_FILE'] . '?p=' . $next ?>">下一页</a><a href="<?= $_SERVER['PHP_FILE'] . '?p=' . $pages ?>">尾页</a><?php endif ?></p></body></html>

<?php$pdo = new PDO("mysql:host=localhost;dbname=user;", "root", "root");$action = $_GET['action'];$id = $_GET['id'];$page = $_GET['p'];// echo $action, $id;// var_dump($pdo->query("SELECT * FROM apple where `id`=$id")->fetch(PDO::FETCH_ASSOC));// var_dump($a);switch ($action) {case 'action':include 'yonghu.php';break;case 'doedit':if (!empty($_POST)) {$name = $_POST['name'];$sex = $_POST['sex'] ? '男' : '女';$sql = "UPDATE `apple` SET `username`=?, `sex`=? WHERE `id`=?";$stmt = $pdo->prepare($sql);$stmt->execute([$name, $sex, $id]);if ($stmt->rowCount() == 1) {echo "<script>alert('编辑程功');location.href='0803.php?p='+'$page'</script>";}}break;case 'del':if ($pdo->query("DELETE FROM `apple` WHERE `id`=$id")->rowCount() == 1)echo "<script>alert('删除成功');location.href='0803.php?p='+'$page'</script>";break;}
<?php$user = $pdo->query("select * from apple where id=$id")->fetch(PDO::FETCH_ASSOC);?><!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><form action="<?= $_SERVER['PHP_FILE'] . '?action=doedit&id=' . $id . '&p=' . $page ?>" method="POST"><fieldset><legend>用户编辑</legend><label for='name'>用户名:</label><input type='text' name='name' id='name' placeholder='<?= $user['username'] ?>' required><label for='sex'>性别:</label><input type='radio' name='sex' id='sex' value='1'>男<input type='radio' name='sex' id='sex1' value='0'>女<button>保存</button></fieldset></form></body></html>


1.了解分页条效果的处理(但感觉冗余了不知怎么精简)
2.删除数据对于是否删除的弹框不知该如何插入
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号