Small example of php paging code and paging principle
- If ($ _ get ['count']) {
- $ Count = $ _ get ['count'];
- } Else {
- $ Count = select count (*) from tablename where .....
- }
-
If only the first page is used to calculate the total number of pages, the subsequent pages will not be used, which improves the efficiency. There is also an application in the case of fuzzy query. I estimate that most of the query results are smaller than 20, that is, there is only one page of results, therefore, it is unnecessary to calculate the total number and the efficiency of fuzzy search is relatively low. So I suddenly thought out of my original thinking. why do we have to calculate the total number of rows and then retrieve the list? You can query the list first. if the number of results in the list is 20, you can query the total number of rows. if the number is less than 20, there is only one page. The total number of rows is equal to the number of results in the list. Pseudocode:
- If ($ _ get ['Page'] <2 ){
- $ List = select * from tablename where... When the first page is limit, the first 20 items are directly queried.
- If (count ($ list) = 20 ){
- $ Count = select count (*) from tablename where .....
- } Else {
- $ Count = count ($ list );
- }
- } Else {
- $ Count = $ _ get ['count'];
- $ List = select * from tablename where... Limit page-1*20, page-1*20 + 20
- }
|