<?php /** * 实现分页功能 */ header("Content-type:text/html; charset=utf-8"); //连接数据库 $pdo = new PDO('mysql:dbname=test','root','root'); $sql = "SELECT id,name,email FROM user ORDER BY id ASC LIMIT :offset,:num"; $pdoStmt = $pdo->prepare($sql); //获取传递的数据 $page = isset($_GET['page'])?$_GET['page']:1; // $page = 1; $offset = ($page-1)*5; $num = 5; $pdoStmt->bindParam(':offset',$offset,PDO::PARAM_INT); $pdoStmt->bindParam(':num',$num,PDO::PARAM_INT); $pdoStmt->execute(); echo '<h2 align="center">用户信息表</h2>'; echo '<table align="center" border=1 cellspacing="0" width="50%" cellpadding="2">'; echo '<tr bgcolor="skyblue"><th>ID</th><th>姓名</th><th>邮箱</th></tr>'; foreach($pdoStmt as $row){ echo '<tr align="center">'; echo '<td>'.$row['id'].'</td>'.'<td>'.$row['name'].'</td>'.'<td>'.$row['email'].'</td>'; echo '</tr>'; } $pdoStmt2 = $pdo->prepare("SELECT id,name,email FROM user"); $pdoStmt2->execute(); $totalPage = ceil($pdoStmt2->rowCount()/$num); echo '</table>'; echo '<h3 align="center">'; echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page=1"> 首页 </a>'; echo '<a style="text-decoration:none; color:'; echo ($page==1)?'grey':''; echo '" href="http://www.test.cn/180105hw/page.php?page='; echo ($page ==1)?1:($page-1); echo '"> 上一页 </a>'; for($i=1;$i<=$totalPage;$i++){ echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page='.$i.'"> '.$i.' </a>'; } echo '<a style="text-decoration:none; color:'; echo ($page==$totalPage)?'grey':''; echo ' " href="http://www.test.cn/180105hw/page.php?page='; echo ($page==$totalPage)?$totalPage:($page+1); echo '"> 下一页 </a>'; echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page='.$totalPage.'"> 尾页 </a>'; echo '</h3>';