效果图:
php:
<?php //引入公用头部页面 include 'public/header.php'; header('Conncet-type:text/html;charset=urf-8'); //获取get $page = isset($_GET['p']) ? $_GET['p'] : 1; //每页显示数据条数 $num = 4; //分页页数 $offset = ($page-1)*$num; //连接数据库,创建pdo对象 $pdo = new PDO('mysql:dbname=demo','root','root'); //创建sql查询语句 $sql = "SELECT * FROM `user` LIMIT :offset,:num"; //执行sql查询语句,创建一个PDOStatement对象 $pdoStmt = $pdo->prepare($sql); //绑定参数 bindParam() $pdoStmt->bindParam(':offset',$offset,PDO::PARAM_INT); $pdoStmt->bindParam(':num',$num,PDO::PARAM_INT); //执行PDOStatement对象 $pdoStmt->execute(); //循环遍历数据 echo '<table class="table table-bordered table-hover text-center">'; echo '<h3 class="text-center">用户信息表</h3>'; echo '<tr><td>ID</td><td>用户名</td><td>邮箱</td><td>密码</td></tr>'; foreach ($pdoStmt as $row) { echo '<tr>'; echo '<td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['password'].'</td>'; echo '</tr>'; } echo '</table>'; //创建sql查询语句 $sql2 = "SELECT * FROM `user`"; //执行sql查询语句,创建一个PDOStatement对象 $pdoStmt2 = $pdo->prepare($sql2); //执行PDOStatement对象 $pdoStmt2->execute(); //echo '表中共有:'.$pdoStmt2->rowCount(); $totalPage = ceil($pdoStmt2->rowCount()/$num); //ceil()向上取整 //上一页 $page = ($page == 0) ? 1 : $page; //下一页 $page = ($page == $totalPage) ? ($totalPage-1) : $page; //分页条 echo '<ul class="pagination" id="page">'; echo '<li><a href="http://localhost/php/pdo_y_f.php?p=1">首页</a></li>'; echo '<li><a aria-label="Previous" href="http://localhost/php/pdo_y_f.php?p='; echo (($page-1)==0)?1:($page-1); echo '">上一页</a></li>'; for ($i=1;$i<=$totalPage;$i++) { echo '<li><a href="http://localhost/php/pdo_y_f.php?p='.$i.'">'.$i.'</a></li>'; } echo '<li><a aria-label="Next" href="http://localhost/php/pdo_y_f.php?p='.($page+1).'">下一页</a>'; echo '<li><a href="http://localhost/php/pdo_y_f.php?p='.$totalPage.'">尾页</a></li>'; echo '</ul>'; //引入公用底部页面 include 'public/footer.php';