This article introduces how to use php to create simple paging (read records from the database. If you need a friend, please refer to the new PHP beginner. I 've been forgetting to do some paging work. Today I am lucky to be reminded to go online and search for it. 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:
The 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. = "homepage | previous page ";
}
$ Page_string. = "| $ page | ";
If ($ page = $ page_count)
{
$ Page_string. = "next page | last page ";
}
Else
{
$ Page_string. = "next page | last page ";
}
$ 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.