Correction status:qualified
Teacher's comments:
总结:
连接数据库获取到全部的记录
导入分页函数库
调用分页函数
如果当前变成为了0,则强制修改为1,否则就是当前页数
如果大于总页数,则强制修改为总页数,否则就是当前页数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> table tr td{ border: 1px solid black; } table tr th{ border: 1px solid black; background-color: lightskyblue; } table{ border-collapse: collapse; width: 70%; margin: 30px auto; text-align: center; } h4{ text-align: center; } a{ text-decoration: none; margin-left: 10px; } a:hover,.active{ background-color: #f0f0f0; font-size: 1.1em; /*color: white;*/ } form{ display: inline; } </style> </head> <body> <?php $db = mysqli_connect('127.0.0.1','root','123.'); mysqli_select_db($db,'php'); $num = 5; $page = isset($_GET['p']) ? $_GET['p'] :1; $offset = ($page-1) * $num; $sql = "SELECT * FROM staff LIMIT {$offset},{$num};"; $res = mysqli_query($db,$sql); $rows = mysqli_fetch_all($res,MYSQLI_ASSOC); $number = mysqli_query($db,"SELECT COUNT(*) FROM staff"); list($total) = mysqli_fetch_row($number); $pages = ceil($total / $num); ?> <table> <caption><h2>员工信息表</h2></caption> <tr> <th>id</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>工资</th> </tr> <?php foreach ($rows as $row): ?> <tr> <td><?php echo $row['staff_id'] ?></td> <td><?php echo $row['name'] ?></td> <td><?php echo $row['sex'] ?></td> <td><?php echo $row['age'] ?></td> <td><?php echo $row['salary'] ?></td> </tr> <?php endforeach; ?> </table> <h4> <?php if($page !=1): ?> <a href="http://127.0.0.1/php/php_5.php?p=1">首页</a> <a href="http://127.0.0.1/php/php_5.php?p= <?php echo (($page-1)==0) ? 1 : ($page-1); ?> ">上一页</a> <?php endif; ?> <?php for ($i=1; $i <=$pages ; $i++): ?> <a class="<?php if($page==$i) echo 'active';?>" href="http://127.0.0.1/php/php_5.php?p=<?php echo $i; ?>"><?php echo $i;?></a> <?php endfor; ?> <?php if($page !=$pages): ?> <a href="http://127.0.0.1/php/php_5.php?p= <?php echo (($page+1)>$pages) ? $pages : ($page+1); ?>">下一页</a> <a href="http://127.0.0.1/php/php_5.php?p= <?php echo ($pages); ?>">尾页</a> <?php endif; ?> <form action="" method="get">第 <select name="p" id=""> <?php for ($i=1; $i <=$pages ; $i++): ?> <option value="<?php echo $i ?>"<?php if ($page==$i) echo 'selected'; ?>><?php echo $i ?></option> <?php endfor; ?> </select>页 <button>确定</button> </form> </h4> </body> </html>
点击 "运行实例" 按钮查看在线实例