分页的最低限度?
PHP/MySQLi 中最少的分页是多少?我在网上搜索过并尝试学习如何进行分页,但所有示例都很大,所以我想知道是否最少需要多少才能从那里开始,然后努力理解它们.
What's the bare minimum of a pagination in PHP/MySQLi? I have searched online and have tried to learn how to do a pagination but all the examples are huge, so I was wondering if what's the minimum so I can start there and then work my way into understanding them.
我尝试过的地方(不是我看过的所有例子);https://github.com/BenGriffiths/pdo-mysqli-pagination ;http://bluedogwebservices.com/php-pagination-with-mysqli/ ;
Places I have attempted (examples not all that I looked at); https://github.com/BenGriffiths/pdo-mysqli-pagination ; http://bluedogwebservices.com/php-pagination-with-mysqli/ ;
第一个很长(似乎)但对我来说有一定的意义,第二个较短但对我来说意义不大,我还没有想过要寻找视频教程,所以我要去搜索对于那些也是.
The first one was extremely long (seemingly) but it made sense in parts to me, the second one was shorter but made less sense to me, I haven't though of looking for video tutorials yet so I am going to search for those too.
推荐答案
分页比较简单,我会尽量用最简单的方式来描述.
Pagination is relatively simple and I will attempt to describe it in the simplest way I can.
首先,您会在大多数分页场景中遇到 5 个术语或短语(或参数):
To start off with here are 5 terms or phrases (or parameters) that you will come across in most pagination scenarios:
- 限制(或每页结果)
- 偏移量(从哪里开始结果/记录集)
- 当前页面
- 总结果
- 页数
以下面的示例查询为例:
Take the example query below:
SELECT * FROM products WHERE active = 1 LIMIT 0 , 25
在上面的 SQL 中(它应该适用于 mysqli):
In the SQL above (it should work for mysqli):
- 25 => 是限制(或每页结果)
- 0 => 是偏移量(即从结果或记录 0 开始)
翻译成,
give me the results 0 - 24, i.e. the first 25 results
因此,假设您有一个包含 1000 条记录的产品表,并且您需要在网页上显示这些记录,每页仅显示 25 条记录:在您的第一页上,这些将是上述 5 个术语的值:
So, assuming that you had a product table with 1000 records and you needed to display these on a webpage only showing 25 records per page: On your first page these will be the value of the 5 terms above:
- 限制(每页结果)=> 25
- 偏移 => 0
- 当前页面 => 1
- 总结果 => 1000
- 页数 => 总结果/限制 => 40
通常偏移量是根据当前页面和限制动态计算的,因此对于您的产品结果的第 1 页,偏移量将等于:
Usually the offset is calculated dynamically based on the current page and the limit, so for page 1 of your product results, the offset would be equal to:
offset = (current page * limit) - limit
i.e. (1 * 25) - 25 = 0
所以你的 mysqli 查询将是:
so your mysqli query would be:
SELECT * FROM products WHERE active = 1 LIMIT [OFFSET] , [LIMIT]
i.e. SELECT * FROM products WHERE active = 1 LIMIT 0 , 25
对产品结果的第 2 页使用相同的原则,偏移量(或起始结果)为:
Using the same principle for page 2 of your product results, the offset (or start results from) would be:
offset = (current page * limit) - limit
i.e. (2 * 25) - 25 = 25
所以你的 mysqli 查询将是:
so your mysqli query would be:
SELECT * FROM products WHERE active = 1 LIMIT [OFFSET] , [LIMIT]
i.e. SELECT * FROM products WHERE active = 1 LIMIT 25 , 25
翻译成,
give me the results 25 - 49, i.e. the next 25 results starting from record 25
其他术语将具有以下值:
the other terms would have the following values:
- 限制(或每页结果)=> 25
- 当前页面 => 2
- 总结果 => 1000
- 页数 => 40
在大多数简单的分页用例中,唯一改变的参数是偏移量和当前页面.如果您在查询中引入过滤器,那么除了前 2 个过滤器之外,总结果和页数也会发生变化.
In most simple use-cases for pagination, the only parameters that change are the offset and the current page. If you introduce filters into your queries then in addition to the former 2, the total results and the number of pages can change as well.
我希望以上的解释有助于让您对分页有一个基本的了解.
I hope the above explanation has helped to provide you with a basic understanding of pagination.
相关文章