The so-called pagination display,That is, the result set in the database is artificially divided into segments to display,Two initial parameters are needed here:

How many records per page ($pagesize)?

What is the current page ($currentpageid)?

Now just give me one more result set,I can show a specific result.

As for the other parameters,Example:Previous page ($previouspageid), next page ($nextpageid), total pages ($numpages)And so on, can be obtained based on previous knowledge.

Take the mysql database as an example,If i want to extract a piece of content from the table,The sql statement can be used:select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.

First 10 records:select * from table limit 0,10

Records 11 to 20:select * from table limit 10,10

Articles 21 to 30:select * from table limit 20,10


This set of SQL statements is actually a SQL statement that takes each page of data in the table when $pagesize=10. We can summarize such a template:

select * from table limit ($currentpageid-1) * $pagesize, $pagesize

Use this template to substitute the corresponding values ​​and compare the set of SQL statements above to see if that is the case.Once you have figured out the most important question of how to get the data,The rest is just passing parameters,Construct the appropriate sql statement and use php to get the data from the database and display it.Below I will use specific code to illustrate.

Simple code implementation

Please read the following code in detail,Debug and run it once,It's better to modify it once,Plus your own features,Search and so on.

//Establish a database connection
$link=mysql_connect ("localhost", "mysql_user", "mysql_password")
   or die ("could not connect:". mysql_error ());
//Get the current number of pages
if (isset ($_ get ["page"])) {
  $page=intval ($_get ["page"]);
else {
//quantity per page
//Get the total amount of data
$sql="select count (*) as amount from table";
$result=mysql_query ($sql);
$row=mysql_fetch_row ($result);
$amount=$row ["amount"];
//count how many pages are in total
if ($amount) {
  if ($amount < $page_size) {$page_count=1;} //If the total amount of data is less than $pagesize, then only one page
  if ($amount%$page_size) {//take the remainder of the total data divided by the number of pages
    $page_count=(int) ($amount/$page_size) + 1;//If there is a remainder,Then the number of pages is equal to the total amount of data divided by the number of pages per page rounded up plus one
  } else {
    $page_count=$amount/$page_size;//If there is no remainder,Then the number of pages is equal to the total data divided by the number of pages
else {
//page link
if ($page == 1) {
  $page_string.="First page | Previous page |";
else {
  $page_string.="First page |. ($page-1).">Previous page | ";
if (($page == $page_count) || ($page_count == 0)) {
  $page_string.="Next Page | Last Page";
else {
  $page_string.=". ($page + 1).">Next page |. $page_count. ">Last page";
//retrieve data,Returns the result as a two-dimensional array
if ($amount) {
  $sql="select * from table order by id desc limit". ($page-1) * $page_size. ", $page_size";
  $result=mysql_query ($sql);
  while ($row=mysql_fetch_row ($result)) {
    $rowset []=$row;
} else {
  $rowset=array ();
//no code to display the result,That is not in the scope of discussion,Just use foreach to easily display the results using the two-dimensional array obtained.
  • Previous Talking about the use of switch and fallthrough statements in Swift programming
  • Next JS uses cookies to display DIV prompt box only once