問題描述
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.
這篇關于分頁的最低限度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!