Php paging code and paging principle

Source: Internet
Author: User

/*
Paging is very simple. Next we will mainly talk about paging principles and implementation methods.
*/
/*
In this way, we can know several pages.
For example, if the result of a list is 126 rows and 20 rows on one page are divided into 7 pages, right.
Our code manager is like this:
Total number of calculated rows: select count (*) from tablename where .....
Query the list select * from tablename where... Limit...
We can see that the first list is re-queried in this way without any optimization.
First, let's assume that the data update frequency is not very high. In this case, click 1st pages and 2nd pages... In fact, the first SQL statement on page n produces the same result. Is it a duplicate of the SQL statement. After we get the number of results on page 1, can we pass the results.
For example, if we link page 2nd to list. php? Page = 2 & count = 126
Add a judgment to the program:
If ($ _ get ['count']) {
$ Count = $ _ get ['count'];
} Else {
$ Count = select count (*) from tablename where .....
}
After this optimization, if we only calculate the total number of pages on the first page, and the number of pages on the next page will not be used, then the efficiency will be improved.
In the case of fuzzy queries, we have an application. 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?
In fact, you can query the list first. If the number of results in the list is 20, we 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
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.