Let's talk about the paging principle and implementation of PHP @ MYSQL _ PHP Tutorial

Source: Internet
Author: User
Let's talk about the paging principle and implementation of PHP @ MYSQL. Let's talk about the paging principle and implementation of PHPMYSQL. before reading this article, please make sure that you have mastered some PHP knowledge and the basic MYSQL Query operation. As a Web program, we often have to talk about the paging principle and implementation of PHP and MYSQL.

Before reading this article, make sure that you have mastered some PHP knowledge and the basics of MYSQL Query operations.

As a Web program, you often have to deal with countless pieces of data, such as member data and article data. if there are only a few dozen members, you can easily display them on one page, however, if your website is thousands or even hundreds of thousands of members, if you open it on one page, it will be a torment for both browser and viewer.

I believe that every beginner who learns PHP will feel a headache for paging, but with this silent post, you will surely pat your head and say, hey, is paging so simple? Indeed, please take a deep breath of fresh air and listen carefully to give you a 1.1-point breakdown.

Suppose we want to process 1000 pieces of data and display 10 pieces of data on each page. this will be displayed on 100 pages, let's take a look at how 10 pieces of information are extracted from mysql.

Select * from table limit 0, 10

The above is a simple mysql Query statement, which extracts 10 pieces of data from a table and obtains the values of all fields.

The key point is in this section "limit", where 0 is the starting point of 0, and 10 is the display of 10 data records, so we should start with 10, how can I write 20th data records?

There may be a lot of people who say "limit 10, 20" right away! Ah, this is wrong. the correct method is "limit 10, 10". The parameter next to it is not the end point but the number to be extracted. remember.

If you know how to extract 10 pieces of data, extract 1000 pieces of data, that is, perform the following query:

Limit 0, 10 // page 1
Limit 10, 10 // page 2
Page 20, 10 // third
Limit 30, 10 // page 4
......
Are there any rules? Yes, the first parameter is increased by 10 every time you flip the page, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter based on the number of pages, we can display the data by page. how is the principle simple?

But how can we change the value of the first parameter based on the number of pages? First, we need to have a page number value, which is obtained using the url GET method.
For example, index. php? Page = 18
I believe most of them are familiar with this topic. this url can be seen everywhere. the page parameter is used to input the page number to be displayed.

Let's use a piece of code to see how it is implemented:
Copy the PHP content to the clipboard. PHP code:

/*

Author: Silent
Date: 2006-12-03

*/

$ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1.
$ Num = 10; // 10 data entries per page

$ Db = mysql_connect ("host", "name", "pass"); // create a database connection
$ Select = mysql_select_db ("db", $ db); // select the database to operate

/*
First, we need to obtain the actual amount of data in the database to determine the specific number of pages to be divided. the specific formula is
The total number of data divided by the number of items displayed on each page, more than one.
That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter.
*/

$ Total = mysql_num_rows (mysql_query ("select id from table"); // The total number of queried data. id is an automatically assigned field in the database.
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages

// If the input page number parameter is greater than the total page number, the error message is displayed.
$ Totalrecord = mysql_num_rows ($ result );
$ Pagesize = 10;
$ Page = isset ($ _ GET ['Page'])? $ _ GET ['Page']: 1;
$ Pagecount = ($ totalrecord % $ pagesize = 0 )? $ Totalrecord/$ pagesize :( int) ($ totalrecord/$ pagesize) + 1;
$ Page = ($ page> $ pagecount | $ page <1 )? 1: $ page;
$ Start = $ pagesize * ($ page-1 );
$ Res = mysql_query ("select * from tablename order by id desc limit $ start, $ pagesize ")

// Obtain the data to be displayed for the corresponding page number. name is a field in the data.
While ($ rsmysql_fetch_array ($ res )){
Echo $ rs ['name']."
";
} // Display Data

I used for below, so I won't talk about it. there are many other methods.

/* Display the page information. if the page is displayed, the numbers in bold are displayed, and the remaining pages are hyperconnections. if the page is currently on the third page, the following information is displayed:
1 2 3 4 5 6
*/
?>

If you carefully read the above code and replace the database connection and query table with yours, you can see the execution result.

Before reading this article, ghost should ensure that you have mastered some PHP knowledge and basic MYSQL Query operations. As a Web program...

Related Article

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.