New to PHP, I always wanted to forget all pages. I was lucky to be reminded today that I searched the internet. Some of them cannot be read. Finally, find a simple one.
The general idea is:
1. Set the maximum number of records to be displayed on each page.
2. calculate the total number of pages
3. The current page and the total number of pages change the connection status
4. Use limit to control reading records from the database
The following code is used:
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']) // checks whether the page parameter exists and obtains the page value. Otherwise, 1 is obtained.
{
$ Page = intval ($ _ GET ['page']);
}
Else
{
$ Page = 1;
}
$ Page_size = 2; // maximum number of records
$ SQL = "SELECT count (*) as amount FROM tb_BookInfo ";
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ result );
// Calculate the total number of 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;
}
// Flip Link
$ Page_string = "";
If ($ page = 1)
{
$ Page_string. = "homepage | previous page ";
}
Else
{
$ Page_string. = "<a href = '? Page = 1 '> homepage </a> | <a href = '? Page = ". ($ page-1)." '> previous page </a> ";
}
$ Page_string. = "| $ page | ";
If ($ page = $ page_count)
{
$ Page_string. = "next page | last page ";
}
Else
{
$ Page_string. = "<a href = '? Page = ". ($ page + 1)." '> next page </a> | <a href = '? Page = $ page_count '> last page </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 method. If you have other methods, please let us know.