Paging 1. The premise of pagination is that records are sorted by ID and are not sequential, for example, some records are deleted,
Or you want to page out the lookup results so that you have a condition that is not paged $q
2. How to determine pagination:
(1): With simple "first page, previous page, next page".
(2): Use "1,2,3,4,5,6,.......... End "To specify that you want to jump to a page.
3. Implementation analysis:
(1) If you first query the full results, only the part of the display. This is obviously not a good way,
Will be tired of the server.
(2) for limit m,n implementation of paging, some are not responsible, the server in the actual operation or
All results are found by $q condition, and then only n after M is returned. The server still works a lot.
(3) The optimization method is to know to display the beginning of the page $id, query
"Where $q and id>= $id ORDER by id desc limit 0, $page _length"
This way MySQL first finds the qualifying ID by the index of the ID, and then evaluates the $q.
(4) How does the $id come?
(5) For display mode 1, more than one query per page, the last record of the $id is a
"Where $q and id>= $id order by ID limit 0, $page _length+1"
if (mysql_num_rows ($result) > $page _length) echo "Next page"
(Remember the last record don't show!)
If you do not use the second paging method, this concludes.
(5) for display Mode 2, the start ID of each page of the following $page_offset=6 page should be known once.
"SELECT ID from xxxx where $q ORDER by id desc limit 0, $page _length* $page _offset"
For ($i =0 $i * $page _length< $mysql _num_rows ($result); $i + +) {
$start =mysql_result ($result, $i * $page _length,0);
Echo ' <a href= ' xxxxx?pageno= '. ($i + 1). " &id= $start \ ">";
if ($id = = $start) echo "<b> $i </b>"; Increase display current page number
else echo $i;
echo "</a>";
}
(6) Maybe someone asked the server not to search all these pages by $q conditions?
and "Limit 0, $page _length* $pageno" What is the difference? Directly with $pageno, where is $id so troublesome?
The answer is to use the session function to save this result, if the $q has not changed, you can call directly,
Saves the database every time you change pages.
Plus subsequent page judgments, the example above becomes:
If $q does not change
$page _offset=6;
Session_register ($ids);
if (! $ids) {//The subsequent page will not be executed
"SELECT ID from xxxx where $q ORDER by id desc limit 0, $page _length* $page _offset+1";/To determine whether there is a subsequent page
For ($i =0 $i * $page _length< $mysql _num_rows ($result); $i + +) {
$ids []=mysql_result ($result, $i * $page _length,0);
}
}
Have Le $ids ...
for ($i =0; $i < $page _offset; $i + +) {
Echo ' <a href= "xxxxx?id=" $ids [$i]. ' " > ';
if ($d = = $ids [$i]) echo "<b> $i </b>"; Increase display current page number
else echo $i;
echo "</a>";
}
The following sentence is free to play, you can switch to paging mode 1
if ($ids [$page _offset])
Echo ' <a href= ' xxxx?pageno= '. ($pageno + 1). ' &id= '. $ids [$page _offset]. " >....</a> ';
(8) The result of the above changes slightly, can Session_resiter ($pageno), to record the current is the first few
Big page. Similar to fast forward function.
(7) Do not know whether the official version of PHP4 to support the hosting of the session array, if not recommended
Implode/explode to become string save.
(8) The advantages of this approach should be fast, but the disadvantage is not knowing the total number of $q conditions.
It should be useful for searching a large database.
(9) Implement "Jump to the end", you can in the above SQL statements order by ID, do not desc. The same can be achieved
Front nth page.
More than 4 of the code is a vision, I hope you can correct me.
5 The main idea of this article is to use the ID index and ID offset to quickly find the following content, save the database cost.
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.