PHP Novice, has always wanted to do a page to forget, today was fortunate to be reminded of the Internet search a bit. Some 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 and the total number of pages compared to change the status of the connection
4. Use limit control to read records from the database
Here's the code:
Copy CodeThe code is as follows:
$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Db_bookstore ', $conn);
mysql_query ("Set names UTF8");
if (Isset ($_get[' page ')))//Determine if the page parameter is present, to get the value of the pages, 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 link
$page _string = "";
if ($page = = 1)
{
$page _string. = "Home | Previous Page ";
}
Else
{
$page _string. = "Home | Previous Page ";
}
$page _string. = "| $page | ";
if ($page = = $page _count)
{
$page _string. = "Next Page | Last ";
}
Else
{
$page _string. = "Next Page | Last ";
}
$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 a very simple way, you road people, if there are other types of methods, please tell it.