批改状态:合格
老师批语:
<?phpnamespace _0819;// 将数据填充到前端的代码表格中// fetch + while// fetchALL+ foreach// bindValue 值绑定到变量上// bindParam 引用绑定到变量上// bindColumn 将字段绑定到变量上// 'SELECT `id`,`name`,`email` FROM `staff`';use PDO;$db = new PDO('mysql:dbname=bittel','root','root');// herdoc$sql = <<< SQLSELECT `id`,`name`,`sex`,`email`FROM `staff`;SQL;// sql语句对象预处理$stmt = $db->prepare($sql);// 执行sql$stmt->execute();// while,foreach, list()进行结果数组的解构到变量中// 0 1 2 3 替换 bindColumn里面第一个数的值$stmt->bindColumn('id', $id);$stmt->bindColumn('name', $name);$stmt->bindColumn('sex', $sex);$stmt->bindColumn('email', $email);// 此种展示太简单 不会用的// while ($stmt->fetch() ){// printf('%d:%s,%d,(%s)<br>',$id,$name,$sex,$email);// }// 此次以表格方式展示$table =<<<TABLE<style>table {border-collapse: collapse;width: 90%;min-width: 360px;margin: 30px auto;text-align:center;}table,th,td {border: 1px solid #000;padding: 5px;}table caption {font-size: 18px;margin-bottom: 10px;}table thead {background: lightcyan;}table tbody tr:hover {cursor: pointer;background: #efefef;}</style><table><caption>员工信息表</caption><thead><tr><th>ID</th><th>姓名</th><th>性别</th><th>邮箱</th></tr></thead><tbody>TABLE;// while ($stmt->fetch() ){// printf('%d:%s,%d,(%s)<br>',$id,$name,$sex,$email);// }while ($stmt->fetch(PDO::FETCH_BOUND) ){// 对性别进行处理 0 、1$tsex= $sex?'男':'女';// 拼装 html 代码$table .= <<<TR<tr><td>$id</td><td>$name</td><td>$tsex</td><td>$email</td></tr>TR;}$table .='</tbody></table>';echo $table;
<?php// 获取分页数据namespace _0819;use PDO;// 连接$db = new PDO('mysql:dbname=bittel','root','root');// 1、页数$page = $_GET['p'];echo '当前页数,p:'.$page.'<br>';// 2、每页展示的数据 num =5$num =5 ;echo '当前数量:num'.$num.'<hr>';// 3、偏移量 offset = (页数 -1)*数量$offset =($page - 1) * $num;// 打印当前偏移量echo '当前偏移量 offse:'.$offset.'<hr>';// $sql = <<< SQL$sql = <<< SQLSELECT *FROM `staff`LIMIT $offset, $num;SQL;$stmt = $db ->prepare($sql);$stmt->execute();$staffs = $stmt ->fetchAll(PDO::FETCH_ASSOC);// fetchAll 是二维数组foreach($staffs as $staff){// list($id,$name,....) 简化extract() 解构后得到一系类变量extract($staff) ;printf('%d-%s-%s-%s<br>', $id, $name, $sex, $email);}
<?php// 获取总页数namespace _0819;use PDO;// 连接$db = new PDO('mysql:dbname=bittel','root','root');// 1、页数 必须要给一个默认页数值,。不然报错 用两个??赋值$page = $_GET['p']??1;echo '当前页数,p:'.$page.'<br>';// 2、每页展示的数据 num =5$num =5 ;echo '当前数量:num'.$num.'<hr>';// 3、偏移量 offset = (页数 -1)*数量$offset =($page - 1) * $num;// 打印当前偏移量echo '当前偏移量 offset:'.$offset.'<hr>';// 4、计算总记录数//方式一: SELECT COUNT('id') FROM `staff`;// 方式二:SELECT COUNT(*) AS `total` FROM `staff`; 起别名// 计算页数 一步到位:SELECT CEIL( COUNT(*)/5) AS `total` FROM `staff`;// 推荐$sql =' SELECT COUNT(*) AS `total` FROM `staff`;' ;$stmt = $db ->prepare($sql);$stmt->execute();// 将总数量绑定到一个变量上$stmt ->bindColumn('total',$total);$stmt->fetch();echo'当前的总记录数:$total:'.$total.'<hr>';// 5. 计算总页数// 向上取整$pages = ceil($total / $num);echo '当前总页数: pages = ' . $pages . '<hr>';$sql = <<< SQLSELECT *FROM `staff`LIMIT $offset, $num;SQL;$stmt = $db->prepare($sql);$stmt->execute();$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);// 在foreach遍历前增加判断是否存在数据if(count($staffs)===0){echo '查询结果为空';}else{foreach($staffs as $staff){extract($staff);printf('%d--%s---%s----%s<br>',$id,$name,$sex,$email);}}function createPages(int $page, int $pages): array{// 当前是第8页, 共计20页// [1, ... 6, 7, 8, 9, 10, .... 20]// 当前是第10页, 共计20页// [1, ... 8, 9, 10, 11, 12, .... 20]// 1. 生成与总页数长度相同的递增的整数数组$pageArr = range(1, $pages);// 2. 只需要当前和前后二页, 其它页码用 false/null 来标记$paginate = array_map(function ($p) use ($page, $pages) {return ($p == 1 || $p == $pages || abs($page-$p) <=2) ? $p : '...';}, $pageArr);// dump($paginate);// 去重, 替换$before = array_unique(array_slice($paginate, 0, $page));$after = array_unique(array_slice($paginate, $page));// 用解构进行合并return [...$before, ...$after];}echo '<hr>';$pgnull = createPages($page, $pages);print_r($pgnull);// array_map ($pgnull as $pg ){// if ( $pg===null){// return '...';// }else {// return $pg;// }// }
<?php// 加载zuoye3.phprequire 'zuoye3.php';echo'<hr>';?><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>分页展示数据</title><style>table {width: 400px;border-collapse: collapse;text-align: center;}table th,table td {border: 1px solid;padding: 5px;}table thead {background-color: lightcyan;}table caption {font-size: larger;margin-bottom: 8px;}body>p {display: flex;}p>a {text-decoration: none;color: #555;border: 1px solid;padding: 5px 10px;margin: 10px 2px;}/* 高亮 */.active {background-color: seagreen;color: white;border: 1px solid seagreen;}</style></head><body><table><!-- 标题 --><caption>员工信息表</caption><!-- 表头 --><thead><tr><td>ID</td><td>姓名</td><td>性别</td><td>邮箱</td></tr></thead><tbody><?phpforeach ($staffs as $staff):extract($staff);?><tr><td><?=$id?></td><td><?=$name?></td><td><?=$sex?></td><td><?=$email?></td></tr><?php endforeach ?></tbody></table><!-- 分页条< ?php for(): ?>< < ?php endfor ? >--><p><!-- 页面地址 --><!-- <?php echo $_SERVER['PHP_SELF'] . '?p=' . 3; ?> --><?php for($i=1;$i<= $pages;$i++): ?><!-- / / 页面跳转的 url --><?php $url = $_SERVER['PHP_SELF'] . '?p=' . $i;// 高亮设置 实现页面高亮 注意类型转换 将$_GET['p'] 字符串型改为整数型// $active = ($i===Intval($_GET['p'])) ? 'active' : null;// 注意要个 第一页给个默认值 ,值为空的时候给默认值,有值不用// 目前还未做页码检查 ???? 自行考虑$page = $_GET['p']??1;$active = ($i===Intval($page)) ? 'active' : null;?><!-- 把地址塞到分页条 生成分页条样式 生成分页数 --><a href="<?=$url?>" class="<?=$active?>"><?=$i?></a><?php endfor ?></p></body></html>
<?php// 加载zuoye3.phprequire 'zuoye3.php';echo'<hr>';?><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>分页展示数据</title><style>table {width: 400px;border-collapse: collapse;text-align: center;}table th,table td {border: 1px solid;padding: 5px;}table thead {background-color: lightcyan;}table caption {font-size: larger;margin-bottom: 8px;}body>p {display: flex;}p>a {text-decoration: none;color: #555;border: 1px solid;padding: 5px 10px;margin: 10px 2px;}/* 高亮 */.active {background-color: seagreen;color: white;border: 1px solid seagreen;}</style></head><body><table><!-- 标题 --><caption>员工信息表</caption><!-- 表头 --><thead><tr><td>ID</td><td>姓名</td><td>性别</td><td>邮箱</td></tr></thead><tbody><?phpforeach ($staffs as $staff):extract($staff);?><tr><td><?=$id?></td><td><?=$name?></td><td><?=$sex?></td><td><?=$email?></td></tr><?php endforeach ?></tbody></table><!-- 分页条< ?php for(): ?>< < ?php endfor ? >--><p><?php$uppage = $page-1;$upurl= $_SERVER['PHP_SELF'] . '?p=' . $uppage;?><?php if($uppage>0):?><a href=<?=$upurl?> >上一页</a>}<?php endif ?><?php foreach($pgnull as $pg): ?><?phpif($pg==='...'){$url='#';}else {$url= $_SERVER['PHP_SELF'] . '?p=' . $pg;}$page = $_GET['p']??1;$active = ($pg===Intval($page)) ? 'active' : null;?><a href="<?=$url?>" class="<?=$active?>"><?=$pg?></a><?php endforeach ?><?php$nextpage = $page + 1;$nexturl= $_SERVER['PHP_SELF'] . '?p=' . $nextpage;if($nextpage >$pages){// echo '没有了';exit;}?><a href=<?=$nexturl?>>下一页</a></p></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号