分页结果时如何获取查询以转移到后续页面 [英] How to get query to transfer to subsquent pages when paginating results

查看:59
本文介绍了分页结果时如何获取查询以转移到后续页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览该网站上的所有分页问题和答案,在所有冗长的代码和OO解决方案中,该代码是最短和最简单的代码:

I've been going through all the pagination questions and answers on the site, and among all the long-drawn out code and OO solutions, this code is among the shortest and simplest:

<?php  
// insert your mysql connection code here  
$perPage = 10; 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
$startAt = $perPage * ($page - 1);  
$query = "SELECT COUNT(*) as total FROM table"; 
$r = mysql_fetch_assoc(mysql_query($query));  
$totalPages = ceil($r['total'] / $perPage);  
$links = ""; 
for ($i = 1; $i <= $totalPages; $i++) { $links .= ($i != $page )  ? "<a href='index.php?page=$i'>Page $i</a> " : "$page "; }   
$query = "SELECT * FROM table ORDER BY title LIMIT $startAt, $perPage";  
$r = mysql_query($query);  
// display results here the way you want  
echo $links; // show links to other pages 

但是我仍然看不到如何在后续页面上使用更新后的LIMIT重新生成查询.这些消息都没有清楚地表明这一点,无论我尝试使用哪种代码,我都将继续获得空白的第二页.如果有人可以解释如何将查询结果传递到下一页,我将不胜感激.

But I still can't see how to regenerate the query with the updated LIMIT on the subsequent pages. None of the messages make this clear, and I continue to get blank second pages no matter which code I try. I'd really appreciate it if someone could explain how to get the query results to the next pages.

推荐答案

我认为您必须在查询中使用OFFSET令牌.像这样:

I think you have to use the OFFSET token in your query. Like so:

$query = "SELECT * FROM table ORDER BY title LIMIT $perPage OFFSET $perPage * $page"; 

我希望这会有所帮助.

这篇关于分页结果时如何获取查询以转移到后续页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆