PHP Novice, has been trying to do a page to forget, today is fortunate to be reminded so the Internet search a bit. Some of them do not understand and do not see how to read. Finally find a relatively simple.
The general idea is:
1. Set the maximum number of records to display per page.
2. Calculate the total number of pages
3. The current page compares the total number of pages to change the status of the connection
4. Read records from the database with limit control
Here's the code:
Copy Code code as follows:
$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Db_bookstore ', $conn);
mysql_query ("Set names UTF8");
if (Isset ($_get[' page '))//To determine if there is a page parameter, get the pages value, otherwise take 1
{
$page = intval ($_get[' page '));
}
Else
{
$page = 1;
}
$page _size = 2; Maximum number of record bars
$sql = "SELECT count (*) as amount from Tb_bookinfo";
$result = mysql_query ($sql);
$row = Mysql_fetch_array ($result);
Calculate Total Pages
$amount = $row [' Amount '];
if ($amount)
{
if ($amount < $page _size) {$page _count = 1;}
if ($amount% $page _size) {$page _count = (int) ($amount/$page _size) + 1;}
else{$page _count = $amount/$page _size;}
}
Else
{
$page _count = 0;
}
Page links
$page _string = "";
if ($page = = 1)
{
$page _string. = "Home | Previous Page ";
}
Else
{
$page _string. = "<a href= '? page=1" > Home </a> | <a href= '? page= '. ($page-1). "' > Prev </a> ";
}
$page _string. = "| $page | ";
if ($page = = $page _count)
{
$page _string. = "Next Page | Last ";
}
Else
{
$page _string. = "<a href= '? page=". ($page + 1). "' > next Page </a> | <a href= '? page= $page _count ' > Last </a> ';
}
$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;
}
? >
This is just a very simple way, you people of the road, if there are other types of methods, please tell me.