Home >Backend Development >PHP Tutorial >PHP paging code and paging principle_PHP tutorial
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 ….. }
if ($_get['page']<2) { $list = select * from tablename where … limit 0,20 第一页时直接查询前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 }