最小分页数? [英] Bare Minimum of a Pagination?

查看:104
本文介绍了最小分页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP/MySQLi中分页的最低要求是什么?我已经在网上搜索并尝试学习如何进行分页,但是所有示例都很庞大,所以我想知道是否有最低要求,以便我可以从那里开始,然后逐步理解它们.

我尝试过的地方(例如,不是我所看到的全部); https://github.com/BenGriffiths/pdo-mysqli-pagination http://bluedogwebservices.com/php-pagination-with-mysqli/

第一个很长(貌似),但对我来说部分有意义,第二个更短,但对我来说意义不大,我还没有寻找视频教程,所以我将进行搜索对于那些.

解决方案

分页相对简单,我将尝试以最简单的方式对其进行描述.

首先,在大多数分页方案中会遇到5个术语或短语(或参数):

  • 限制(或每页结果)
  • 偏移量(从此处开始结果/记录集的位置)
  • 当前页面
  • 总结果
  • 页数

以下面的示例查询为例:

SELECT * FROM products WHERE active = 1 LIMIT 0 , 25

在上面的SQL中(它适用于mysqli):

  • 25 =>是限制(或每页结果)
  • 0 =>是偏移量(即从结果或记录0开始)

翻译成

give me the results 0 - 24, i.e. the first 25 results

因此,假设您有一个包含1000条记录的产品表,并且需要在网页上显示这些记录,每页仅显示25条记录:在您的第一页上,这些就是上述5个术语的值:

  • 限制(每页显示的结果)=> 25
  • 偏移=> 0
  • 当前页=> 1
  • 总结果=> 1000
  • 页数=>总结果/限制=> 40

通常,偏移量是根据当前页面和限制来动态计算的,因此对于产品结果的第1页,偏移量等于:

offset = (current page * limit) - limit
i.e. (1 * 25) - 25 = 0

所以您的mysqli查询将是:

SELECT * FROM products WHERE active = 1 LIMIT [OFFSET] , [LIMIT]

i.e. SELECT * FROM products WHERE active = 1 LIMIT 0 , 25

对产品结果的第2页使用相同的原理,偏移量(或起始结果来自)为:

offset = (current page * limit) - limit
i.e. (2 * 25) - 25 = 25

所以您的mysqli查询将是:

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

其他术语将具有以下值:

  • 限制(或每页结果)=> 25
  • 当前页面=> 2
  • 总结果=> 1000
  • 页数=> 40

在大多数简单的分页用例中,唯一更改的参数是偏移量和当前页面.如果您在查询中引入过滤器,那么除了前两个查询之外,总结果和页面数也可能会发生变化.

我希望以上解释有助于您对分页有一个基本的了解.

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.

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.

To start off with here are 5 terms or phrases (or parameters) that you will come across in most pagination scenarios:

  • Limit (or results per page)
  • Offset (where to start the set of results/records from)
  • Current page
  • Total results
  • Number of pages

Take the example query below:

SELECT * FROM products WHERE active = 1 LIMIT 0 , 25

In the SQL above (it should work for mysqli):

  • 25 => Is the limit (or results per page)
  • 0 => Is the offset (i.e start from result or record 0)

which translates into,

give me the results 0 - 24, i.e. the first 25 results

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:

  • Limit (results per page) => 25
  • Offset => 0
  • Current page => 1
  • Total results => 1000
  • Number of pages => total results / limit => 40

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

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

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

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

which translates into,

give me the results 25 - 49, i.e. the next 25 results starting from record 25

the other terms would have the following values:

  • Limit (or results per page) => 25
  • Current page => 2
  • Total results => 1000
  • Number of pages => 40

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.

这篇关于最小分页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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