How to get query to transfer to subsquent pages when paginating results(分页结果时如何让查询转移到后续页面)
问题描述
我浏览了网站上所有的分页问题和答案,在所有冗长的代码和面向对象的解决方案中,这段代码是最短和最简单的:
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";
我希望这会有所帮助.
这篇关于分页结果时如何让查询转移到后续页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:分页结果时如何让查询转移到后续页面


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01