Correction status:qualified
Teacher's comments:
分页显示要注意的几点:
要设置每一页显示的记录数量:$pageNum
获取当前的页数:url参数: $page=isset($_GET['p']) ? $_GET['p'] : 1;
获取总记录数:$total 总的记录数量
获取总页数: $pages = ceil($total / $pageNum) //ceil()函数为向上取整
获取每一页的起始索引: 偏移量 : $offset = ($page - 1) * $pageNum
<?php // 员工信息 //1.连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //分页显示 //设置一下每一页显示的记录 $pageNum = 5; //获取当前 的页数:URL参数 $page = isset($_GET['p']) ? $_GET['p'] : 1; //获取总页数 $stmt = $pdo->prepare('SELECT COUNT(*) FROM `staff`'); $stmt->execute(); $total = $stmt->fetchColumn(0); $pages = ceil($total / $pageNum); //ceil()向上取整 //每一页的起始索引:偏移量 $offset = ($page - 1) * $pageNum; //2.执行的操作 $sql = "SELECT * FROM `staff` WHERE `is_show`=1 LIMIT {$offset},{$pageNum}"; $stmt = $pdo->prepare($sql); $stmt->execute(); $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC); //3.关闭连接 $pdo = null; // 标题 $title = '员工信息表'; // 表格标题 $tableTitle = $title; // 员工数量 //$total = count($staffs); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <style> table,th,td { border: 1px solid #666; padding: 8px; } table { border-collapse: collapse; width: 80%; text-align: center; margin: 30px auto; } thead tr:first-of-type { background-color: lightblue; } tbody tr:hover { background-color: #efefef; } table > caption { font-size: 1.2rem; margin-bottom: 15px; } /*.total {*/ /*text-align: center;*/ /*}*/ table ~ p { text-align: center; } table + p a { display: inline-block; width: 50px; height: 24px; border: 1px solid #333; text-decoration: none; } table + p a:hover { background: lightblue; } button:hover { cursor: pointer; background-color: lightblue; } /*添加按钮给个特殊样式*/ #add { height: 25px; width: 90px; position: absolute; left: 650px; top: 40px; } </style> </head> <body> <button onclick="location.href='staff_add.php'" id="add">添加</button> <table> <caption> <?php echo '<span style="color:red">' . $tableTitle . '</span>'; ?> </caption> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>手机</th> <td>入职</td> <th>操作</th> </tr> </thead> <tbody> <!--foreach()替代语法--> <?php foreach($staffs as $staff) : ?> <tr> <td><?php echo $staff['id']; ?></td> <td><?php echo $staff['name']; ?></td> <td><?php echo $staff['age']; ?></td> <!--if()替代语法--> <td> <?php if($staff['sex'] == 1) : ?> 男 <?php else: ?> 女 <?php endif; ?> </td> <!--如果只是简单的输出变量可以使用php短标签语法--> <td><?=$staff['mobile']?></td> <td> <?php echo date('Y/m/d',$staff['hiredate']); ?> </td> <td> <button onclick="location.href='staff_edit.php?id=<?=$staff['id']?>'">编辑</button> <button onclick="return confirm('是否删除?') ? location.assign('staff_manage.php?action=del&id=<?=$staff['id']?>') : false"><span style="color:red">删除</span></button> </td> </tr> <?php endforeach;?> </tbody> </table> <!--<p> <a href="staff_list.php?p=1">1</a> <a href="staff_list.php?p=2">2</a> <a href="staff_list.php?p=3">3</a> <a href="staff_list.php?p=4">4</a> <a href="staff_list.php?p=5">5</a> </p>--> <!--动态生成页码--> <p> <a href="staff_list.php?p=1">首页</a> <?php for($i=1;$i<=$pages;$i++): ?> <!-- <a href="staff_list.php?p=--><?//=$i?><!--">--><?//=$i?><!--</a>--> <!--将当前页码高亮--> <?php if(isset($_GET['p']) && $_GET['p'] == $i) { $bgColor = 'style="background: lightblue"'; } else { $bgColor = ''; } ?> <a href="javascript: location.href='<?=$_SERVER['PHP_SELF']?>?p=<?=$i?>'" <?=$bgColor?>><?=$i?></a> <?php endfor; ?> <a href="staff_list.php?p=<?=$pages?>">尾页</a> </p> <p class="total">总计:<?php echo $total; ?>人</p> </body> </html>
点击 "运行实例" 按钮查看在线实例