How to create a simple paging (read records from the database) using php _ php instance

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.