* 分页查询的原理
* 分析分页的原理:
* 1. limit 参数的作用: 偏移量与显示数量
* 2. 如果控制每页显示的数量
* 3. 接收get参数,用p表示当前页数,每页显示3条
* 4. 需要的参数:
* (1).totalpage 总页数
* (2).totalnumber 一共有多少条数据
* (3).pagesize 每页显示多少条数据
* (4)currentpage 当前第几页
* (5)*.rangestart 起始页
* (6)*.rangeend 末页
* 5. 当前偏移量的计算公式: (页数-1)*每页显示的数量
* offset = (page-1)*num
$page = isset($_get['p'])? $_get['p']:1;$page = ($page == 0 ) ? 1 : $page;$num = 5;$offset = ($page-1)*$num;//1.获取到所有数据,用表格显示出来$pdo = new pdo('mysql:host=localhost;dbname=php','root', 'root');//$sql = "select id,name,email from user1 limit 0,3";$sql = "select `staff_id`,`name`,`sex`,`age`,`salary` from `staff` limit :offset, :num";$stmt = $pdo->prepare($sql);$stmt->bindparam(':offset', $offset, pdo::param_int);$stmt->bindparam(':num', $num, pdo::param_int);$stmt->execute();$stmt->setfetchmode(pdo::fetch_assoc);$res = $stmt->fetchall();echo '<h2 align="center">员工信息表</h2>';echo '<table border="1" cellspacing="0" cellpadding="5" width="70%" align="center">';echo '<tr bgcolor="lightgreen"><th>id</th><th>用户名</th><th>性别</th><th>年龄</th><th>工资</th></tr>';foreach ($res as $row) { echo '<tr align="center">'; echo '<td>'.$row['staff_id'].'</td><td>'.$row['name'].'</td>'; $row['sex'] = $row['sex']==0 ? '男' : '<span style="color:red">女</span>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td><td>'.$row['salary'].'</td>'; echo '</tr>';}echo '</table>';//计算共计多少页?$stmt2 = $pdo->prepare("select * from staff");$stmt2->execute();$totalpage = ceil($stmt2->rowcount() / $num);$page = ($page == $totalpage) ? ($totalpage-1) : $page;echo '<style>a {margin-left: 10px;text-decoration: none}a:hover{color:red}</style>';echo '<h3 align="center">';echo '<a href="http://php.io/0427/page.php?p=1">首页</a>';echo '<a href="http://php.io/0427/page.php?p=';echo (($page-1)==0)?1:($page-1);echo '">上一页</a>';for ($i=1; $i<=$totalpage; $i++) { echo '<a href="http://php.io/0427/page.php?p='.$i.'">'.$i.'</a>';}echo '<a href="http://php.io/0427/page.php?p='.($page+1).'">下一页</a>';echo '<a href="http://php.io/0427/page.php?p='.$totalpage.'">尾页</a>';echo '</h3>';