Home  >  Article  >  Backend Development  >  PHP paging code and paging principle small example

PHP paging code and paging principle small example

WBOY
WBOYOriginal
2016-07-25 08:52:501017browse
  1. if ($_get['count']) {
  2. $count = $_get['count'];
  3. } else {
  4. $count =select count(*) from tablename where …..
  5. }
Copy the code

If only the first page is calculated and the subsequent pages are not used, will this improve efficiency? There is another situation, that is, in the case of fuzzy query, there is an application. I estimate that most of the query results are less than 20, that is, there is only one page of results, so it is unnecessary to calculate the total number, and it is fuzzy. Query efficiency is also relatively low. So I suddenly thought of jumping out of the original thinking, why do we have to calculate the total number of rows first and then get the list?

You can query the list first. If the number of list results = 20, then query the total number of rows, because if it is less than 20, there is actually only one page. The total number of rows is equal to the number of list results found. The pseudo code is:

  1. if ($_get['page']<2) {
  2. $list = select * from tablename where … limit 0,20 Directly query the first 20 items on the first page
  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. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn