先写好了查询页面,如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系统主页面</title> </head> <body> <?php $conn = mysqli_connect("localhost","root",""); if(!$conn){ echo "失败"; } mysqli_select_db($conn,"bbs"); $sql = "select * from bbs_user"; $obj = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>编号</th><th>姓名</th><th>密码</th><th>地址</th><th>性别</th><th>年龄</th><th>操作</th>"; while($row = mysqli_fetch_assoc($obj)){ echo "<tr>"; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['password'].'</td>'; echo '<td>'.$row['address'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo '<td><a href = "del.php?id='.$row['id'].'">删除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>'; echo "</tr>"; } echo "</table>"; echo "<a href = 'add.php'>添加</a>"; echo "<center>"; mysqli_close($conn); ?> </body> </html>
在查询界面上做了如下改变:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系统主页面</title> </head> <body> <?php //5.设置$page的默认值 #$page = 1; //8.修改$page的值 $page = empty($_GET['page'])?1 : $_GET['page']; $conn = mysqli_connect("localhost","root",""); if(!$conn){ echo "失败"; } mysqli_select_db($conn,"bbs"); //------------分页开始------------------- //1.求出总条数 $sql = "select count(*) as count from bbs_user"; $result = mysqli_query($conn,$sql); $pageRes = mysqli_fetch_assoc($result); #var_dump($pageRes); //13 $count = $pageRes['count']; //2.每页显示数(5) $num = 5; //3.根据每页显示数求出总页数 $pageCount = ceil($count / $num); //向上取整 #var_dump($pageCount); //3 //4.根据总页数求出偏移量 $offset = ($page - 1) * $num; //$page默认为 1, 下一步设置 //------------分页结束------------------- //6.修改sql语句 $sql = "select * from bbs_user limit " . $offset . ',' . $num; #$sql = "select * from bbs_user"; $obj = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>编号</th><th>姓名</th><th>密码</th><th>地址</th><th>性别</th><th>年龄</th><th>操作</th>"; while($row = mysqli_fetch_assoc($obj)){ echo "<tr>"; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['password'].'</td>'; echo '<td>'.$row['address'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo '<td><a href = "del.php?id='.$row['id'].'">删除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>'; echo "</tr>"; } echo "</table>"; #echo "<a href = 'add.php'>添加</a>"; echo "<center>"; //10.设置上一页下一页的$prev和$next $prev = $page - 1; $next = $page + 1; //11.设置页数限制 if($prev<1){ $prev = 1; } if($next>$pageCount){ $next = $pageCount; } //关闭连接 mysqli_close($conn); ?> <!--7.添加首页、上一页、下一页、尾页(href没有链接)--> <!--9.给定链接,首页和尾页写死,首页就是page=1,尾页是总页数,上一页先用$prev表示,下一步设置,下一页同上一页--> <a href="testFenye.php?page=1">首页</a> <a href="testFenye.php?page=<?php echo $prev;?>">上一页</a> <!--混编简写--> <a href="testFenye.php?page=<?=$next;?>">下一页</a> <a href="testFenye.php?page=<?=$pageCount;?>">尾页</a> </body> </html>
原文地址:http://blog.51cto.com/13534640/2088289