php?分页

WBOY
Release: 2016-06-23 13:24:41
Original
853 people have browsed it

从数据库的读出N条数据,想对这N条数据进行分页

1,我如何对这些数据进行分页,使它每页显示10条数据
2.如何计算出一共有多少页

希望能给个例子参考下


回复讨论(解决方案)

<?php $rows=mysql_num_rows(mysql_query(你的SQL));echo "总页数:".ceil($rows/10);//向上取整?>
Copy after login

SELECT * FROM table WHERE xxxxxx LIMIT $start_page,10;
$start_page是你的每页的开始条数

1.首先使用select count(*) from table where xxx 获取记录总数
2.(total-1)/10+1 = 总分页数。如果total=0,总分页数就是0

select * from tablename limit 5,10

分页可以这样写

$page = 1; // 要现实第几页的数据$pagesize = 10; // 每页显示10条数据$offset = ($page-1)*$pagesize; // 根据页数与每页记录数,计算偏移数$sqlstr = "select * from table limit ".$offset.",".$pagesize;
Copy after login
Copy after login

分页可以这样写

$page = 1; // 要现实第几页的数据$pagesize = 10; // 每页显示10条数据$offset = ($page-1)*$pagesize; // 根据页数与每页记录数,计算偏移数$sqlstr = "select * from table limit ".$offset.",".$pagesize;
Copy after login
Copy after login


$page,$pagesize,$offset 这些变量要保存在Session里吗?

不要!
$page 是传入的
$pagesize 是在程序里指定的
$offset 是计算出来的

根据总条数确定总页数,然后要多少查多少。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!