批改状态:合格
老师批语:这些底层的东西, 要多看看, 其它高级应用只是它的一个实现罢了,并不神秘,对不对?
数据表的分页查询
<?php require '0912-2.php' ?><?php// 省略号设置:必须将显示的分页码的数量固定下来,当大于这个数量的时候才显示省略号// 新分页条的页码数量:一定是奇数$showPages = 5;// 新分页条的起始页码的初值,默认是1$startPage = 1;// 新分页条的终止页码,默认是总页数$endPage = $pages;// 偏移量:新分页条相当于当前页码的偏移量,当前页码与省略号之间的间距,新分页条的偏移量是2$offsetPage = ($showPages - 1) /2 ;// 新分页条中的页码数量 小于 总页数才出现...if ($showPages < $pages) {// 只有当前的页码 大于偏移量 加1 时,才显示左边的省略标记 ... ;只有当页码大于3的时候才会出现if ($page > $offsetPage + 1) $startOmit = '...';// 如果当前页 大于 偏移量时,需要改变新分页条的起始页码和终止页码if ($page > $offsetPage) {// 当前分页条的起始页码 = 当前页码 - 偏移量(本质就是页码前移)$startPage = $page - $offsetPage;// 如果当前页码是5,起始应为5-2=3// 重置分页条的结束页码 = 当前页码 + 偏移量$endPage = $page + $offsetPage;// 当结束页码越界时,新分页条的终止页码设置为总页数if ($endPage > $pages) {$endPage = $pages;// 起始页码设置为总页数-(展示页数-1)$startPage = $pages - ($showPages - 1);}} else {// 如果当前页码 小于 偏移量;新分页条的起始页是1$startPage = 1;// 新分页条的结束页码 = 展示页数的值$endPage = $showPages;}// 当前页码大于展示页数的值,并且当前页码+偏移量仍小于总页数,显示右边的省略号// 只要当前显示的页码数量, 小于 总页数并且当前页加上偏移量也小于总页数,应该显示出后面的 ...if ($pages > $page && $page+$offsetPage < $pages) $endOmit = '...';}?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>带省略号的分页数据</title></head><style>* {margin: 0;padding: 0;box-sizing: border-box;}ul{list-style:none;}body {display: flex;flex-direction: column;align-items: center;}table{width:80%;border:1px solid;border-collapse: collapse;text-align: center;}table caption {font-size: 1.2rem;margin: 10px;}table td,table th {border: 1px solid;padding: 5px;}table tr:hover {background-color: #eee;}table thead tr:only-of-type {background-color: lightblue;}table button {width: 56px;height: 26px;}table button:last-of-type {color: red;}table button {cursor: pointer;margin: 0 3px;}/* 分页 */.paging>p{display:flex;}.paging >p>a{text-decoration: none;color: #555;border: 1px solid;padding: 5px 10px;margin: 10px 2px;}.paging >p>a.active {background-color: red;color: white;border: 1px solid red;}</style><body><table><caption>用户信息表</caption><thead><tr><th>ID</th><th>姓名</th><th>邮箱</th><th>操作</th></tr></thead><tbody><?php foreach($users as $user): ?><tr><td><?php echo $user['id'] ?></td><td><?=$user['name']?></td><!-- 使用php短标签来快速打印变量 --><td><?php echo $user['email'] ?></td><td><button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">编辑</button><button onclick="del(<?=$user['id']?>)">删除</button></td></tr><?php endforeach ?></tbody></table><div class="paging"><p><!-- 当前页数不等于1时,显示上一页和首页,否则全部隐藏 --><?php if($page!=1) :?><!-- 获取上一页的页码 --><?php $prev=$page-1;if($page<=1) $prev=1 ?><a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首页</a><a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一页</a><!-- 判断是否有值 --><?php if(isset($startOmit)) :?><a href><?=$startOmit?></a><?php endif ?><?php endif ?><?php for($i=$startPage;$i<=$endPage;$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 if($page!=$pages) :?><?php if(isset($endOmit)) :?><a href><?=$endOmit?></a><?php endif ?><!-- 获取下一页的页码 --><?php $next=$page+1;if($page>=$pages) $next=$pages ?><a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一页</a><a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾页</a><?php endif ?></p></div><script>function del(id) {let url = 'http://php.edu/php作业/0912/handle.php?action=delete&id='+id;return confirm('是否删除?') ? location.href= url : false;}</script></body></html>


<?php// 1、连接数据库$dsn = 'mysql:host=localhost;dbname=phpedu';$username = 'root';$password = 'root';$pdo = new PDO($dsn, $username, $password);// 2、获取操作类型$action= $_GET['action'];$id=$_GET['id'];// 3、操作switch ($action){// 第一步 渲染出一个编辑表单case 'edit':// 加载编辑表单include 'edit.php';break;// 第二步 执行编辑操作,将新数据写入数据表中case 'doedit':// 更新操作$sql = 'UPDATE `user` SET `name`=?, `email`=? WHERE `id`=?;';$stmt = $pdo->prepare($sql);if (!empty($_POST)) {$stmt->execute([$_POST['name'], $_POST['email'], $id]);if ($stmt->rowCount() == 1)echo '<script>alert("更新成功");location.href="0912-4.php";</script>';}break;case 'insert'://新增数据$stmt = $pdo->prepare('INSERT INTO `user` SET `name`=?,`email`=?,`password`=?;');$stmt->execute([$_POST['name'], $_POST['email'], sha1($_POST['password'])]);// echo $stmt;if($stmt->rowCount() == 1){echo '<script>alert("添加成功");location.href="0912-4.php";</script>';}break;case 'delete':// 删除操作$stmt = $pdo->prepare('DELETE FROM `user` WHERE `id`=?;');$stmt->execute([$id]);if ($stmt->rowCount() == 1)echo '<script>alert("删除成功");location.href="0912-4.php";</script>';break;}
<?php// 编辑表单$user=$pdo->query('select * from 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="<?php echo $_SERVER['PHP_SELF']. '?action=doedit&id='.$id?>" method="post"><p><label for="name">用户名:</label><input type="text" id="name" name="name" value="<?=$user['name']?>"/></p><p><label for="email">邮箱:</label><input type="text" id="email" name="email" value="<?=$user['email']?>"/></p><p><button>保存</button></p></form></body></html>
<!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="<?php echo 'http://php.edu/php%E4%BD%9C%E4%B8%9A/0912/handle.php?action=insert' ?>" method="post"><p><label for="name">用户名:</label><input type="text" id="name" name="name"/></p><p><label for="email">邮箱:</label><input type="text" id="email" name="email"/></p><p><label for="password">密码:</label><input type="text" id="password" name="password"/></p><p><button>保存</button></p></form></body></html>


<?php//model模型namespace mvc_demo;use PDO;//声明命名空间后需进行use别名设置//数据库操作class Model{//数据获取public function getData(){return (new PDO('mysql:host=localhost;dbname=phpedu', 'root','root'))->query('select id,name,email from user limit 5')->fetchAll(PDO::FETCH_ASSOC);}}?>
<?php//视图namespace mvc_demo;class View{// 将模型中获取的数据进行展示public function fetch($data){$table='<table>';$table .= '<caption>用户信息表</caption>';$table .= '<tr><th>ID</th><th>姓名</th><th>邮箱</th></tr>';// 遍历用户数据foreach ($data as $user) {$table .= '<tr>';$table .= '<td>'.$user['id'].'</td>';$table .= '<td>'.$user['name'].'</td>';$table .= '<td>'.$user['email'].'</td>';$table .='</tr>';}$table .='</table>';return $table;}}echo '<style>table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}caption {font-size: 1.2rem; margin-bottom: 10px;}tr:first-of-type { background-color:yellow;}td,th {border: 1px solid; padding:5px}</style>';?>
<?php//控制器4:将当前依赖的多个对象,放在一个"服务容器"的对象池中进行统一管理namespace mvc_demo;use Closure;//加载模型require __DIR__ .'/Model.php';//加载视图require __DIR__ .'/View.php';//服务容器class Container{//对象容器protected $instances=[];//添加对象:参数1:对象别名;参数2:对象(new)public function bind($alias,Closure $process){$this->instances[$alias]=$process;}//容器内取出对象(实例化)public function make($alias,$params=[]){// 回调执行return call_user_func_array($this->instances[$alias],[]);}}//将依赖的外部对象绑定到服务容器中$container=new Container;//绑定模型、视图$container->bind('model',function(){return new Model();});$container->bind('view',function(){return new View();});//控制器class Controller4{public function index(Container $container){//获取模型中的数据$data = $container->make('model')->getData();//渲染模板视图return $container->make('view')->fetch($data);}}//客户端测试$controller=new Controller4();//实例化echo $controller->index($container);?>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号