PHP分页详解
Code
1$perpagenum = 3;//定义每页显示几条
2$page=$_GET['page'];
3$nextp=$page+1;
4$backp=$page-1;
5$total = mysql_fetch_array(mysql_query(”select count(*) from text”));//查询数据库中一共有多少条数据
6$Total = $total[0]; //
7$Totalpage = ceil($Total/$perpagenum);//上舍,取整
8
9
10if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage||$_GET['page']<1)//page可能的四种状态
11{
12 $page=1;
13}
14else
15{
16 $page=$_GET['page'];//如果不满足以上四种情况,则page的值为$_GET['page']
17}
18
19$startnum = ($page-1)*$perpagenum;//开始条数
20
21$sql =”SELECT *
22FROM `text`
23ORDER BY `text`.`id` DESC limit $startnum,$perpagenum”;//SQL查询
24$result = mysql_query($sql,$conn);
25$row=mysql_fetch_array($result);
26
27
28
29<BODY>
30
31 <div id=”d”>总共有
32 <?php echo “$Total”; ?>
33 条,总共有
34 <?php echo”$Totalpage”; ?>
35 页
36 <a href=”main.php?page=<?php echo $backp ?>” >上一页</a>
37
38 <a href=”main.php?page=<?php echo $nextp ?>” >下一页</a>
39 </div>
40
41 </BODY>
42
43
1$perpagenum = 3;//定义每页显示几条
2$page=$_GET['page'];
3$nextp=$page+1;
4$backp=$page-1;
5$total = mysql_fetch_array(mysql_query(”select count(*) from text”));//查询数据库中一共有多少条数据
6$Total = $total[0]; //
7$Totalpage = ceil($Total/$perpagenum);//上舍,取整
8
9
10if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage||$_GET['page']<1)//page可能的四种状态
11{
12 $page=1;
13}
14else
15{
16 $page=$_GET['page'];//如果不满足以上四种情况,则page的值为$_GET['page']
17}
18
19$startnum = ($page-1)*$perpagenum;//开始条数
20
21$sql =”SELECT *
22FROM `text`
23ORDER BY `text`.`id` DESC limit $startnum,$perpagenum”;//SQL查询
24$result = mysql_query($sql,$conn);
25$row=mysql_fetch_array($result);
26
27
28
29<BODY>
30
31 <div id=”d”>总共有
32 <?php echo “$Total”; ?>
33 条,总共有
34 <?php echo”$Totalpage”; ?>
35 页
36 <a href=”main.php?page=<?php echo $backp ?>” >上一页</a>
37
38 <a href=”main.php?page=<?php echo $nextp ?>” >下一页</a>
39 </div>
40
41 </BODY>
42
43