Php paging code and paging Principle

Source: Internet
Author: User

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 of limit is queried directly, the first 20 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}

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.