Small example of php paging code and paging principle

Source: Internet
Author: User
Small example of php paging code and paging principle

  1. If ($ _ get ['count']) {
  2. $ Count = $ _ get ['count'];
  3. } Else {
  4. $ Count = select count (*) from tablename where .....
  5. }

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:

  1. If ($ _ get ['Page'] <2 ){
  2. $ List = select * from tablename where... When the first page is limit, the first 20 items are directly queried.
  3. If (count ($ list) = 20 ){
  4. $ Count = select count (*) from tablename where .....
  5. } Else {
  6. $ Count = count ($ list );
  7. }
  8. } Else {
  9. $ Count = $ _ get ['count'];
  10. $ List = select * from tablename where... Limit page-1*20, page-1*20 + 20
  11. }

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.