Home >Backend Development >PHP Tutorial >PHP paging code and paging principle_PHP tutorial

PHP paging code and paging principle_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:22:19744browse

This way you can know how many pages it is divided into.
For example, if the list result is 126 lines, if there are 20 lines per page, then it will be divided into 7 pages, right?
Our code manager looks like this:
Calculate the total number of rows: select count(*) from tablename where …..
Query list select * from tablename where … limit…
We see that this method does not have any optimization method, so that the first list will be queried again.
First, let's assume that the data update frequency is not very high, let's click on page 1 and page 2. . . Page n In fact, the first sql sentence gets the same result. Does this mean that the subsequent work will be repeated? Then after we get the number of results on page 1, can we just pass the results on at once?
For example, page 2 of our pagination link is like this 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 on the first page, and the subsequent pages are not used, will this improve efficiency?
There is another situation, that is, in the case of fuzzy query, we have 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 not necessary to calculate the total number, and 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?
In fact, we can query the list first. If the number of list results = 20, then we will 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:
if ($_get[&#39;page&#39;]<2) {
$list = select * from tablename where &hellip; limit 0,20  第一页时直接查询前20条
if (count($list)=20) {
$count =select count(*) from tablename where &hellip;..
} else {
$count =count($list);
}
} else {
$count = $_get[&#39;count&#39;];
$list = select * from tablename where &hellip; limit page-1*20,page-1*20+20
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/447010.htmlTechArticleThis way you can know how many pages it is divided into. For example, if the list result is 126 lines, if there are 20 lines per page, then it will be divided into 7 pages, right? Our code manager looks like this: Calculate the total number of lines: select...
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