前端代码实现员工信息查询
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> table,th,td{border: 1px solid black; } table{ border-collapse: collapse; width: 70%; margin:30px auto; text-align: center; } table th{ background-color: skyblue; } h3{ text-align: center; margin-left: 20px; } h3 a{ text-decoration: none; border: 1px solid #3c3c3c; padding: 5px; background: #bce8f1; } h3 a:hover, .active { color: red; } </style> </head> <body> <?php require 'lib/func_page.php'; $db = mysqli_connect('127.0.0.1', 'root', 'root'); mysqli_select_db($db, 'php'); //$num = 5; $table = 'staff'; $page = isset($_GET['p'] )?$_GET['p']:1; $data = func_page($db,$table,$page,$num=5); $rows = $data['rows']; $pages = $data['pages']; ?> <table> <caption>员工信息表</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> <h3> <?php for ($i=1;$i<=$pages;$i++): ?> <a <?php if ($page == $i )echo 'class = active'; ?> href="http://www.bb.io/php/180427/demo4.php?p=<?php echo $i ?>"><?php echo $i ?></a> <?php endfor;?> </h3> </body> </html>
分页函数库封装,调用该函数执行分页
<?php if (!function_exists('func_page')){ function func_page($db,$table,$page,$num=5){ $offset = ($page-1)*5; $sql = "SELECT * FROM $table 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); return ['rows'=>$rows, 'pages' => $pages,]; } } ?>
总结经过这学习,了解了数据库数据输出原理,以及计算总行数的过程
ceil函数向上取整,list函数的使用,使用全局变量$_GET 获取页数,当前页数高亮,以及分页跳转