php – mysql – 在分页中查找结果记录页面

前端之家收集整理的这篇文章主要介绍了php – mysql – 在分页中查找结果记录页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

想象我们使用分页来分割和显示像这样的mysql结果,
自动inceremental ID和日期排序:

SELECT name FROM members ORDER BY id DESC,date DESC LIMIT $start,$len

我们使用PHP在它下面显示结果和页面导航链接.

我们如何找到记录ID号x在该结果的哪个页面中,以便我们将页码设置到该页面显示页面和最终用户不需要单击导航并找到它?

最佳答案
首先获得记录总数.

select count(*) as total from members; 

在记录列表中找到行成员“x”的编号

select count(*) oneLess from members where id < (select id from members where name='x');

上面的查询返回一个来自x的记录号.即’x’是1Less 1

现在计算页码.

$asc_page_no =  floor((($oneLess+1)/$total)*$len);
$total_pages = floor($total/$len);
$page_no = $total_pages - $asc_page_no; //reverse the page looking direction

然后计算$start

$start = $page_no * $len;
原文链接:https://www.f2er.com/mysql/433170.html

猜你在找的MySQL相关文章