Home  >  Article  >  Backend Development  >  Silently talk about PHP&MYSQL paging principle and implementation_PHP tutorial

Silently talk about PHP&MYSQL paging principle and implementation_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:55786browse

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

As a web program, it often has to deal with countless data, such as member data and article data. If there are only a few dozen members, it is easy to handle. It can be displayed on one page. But what if If your website has thousands or even hundreds of thousands of members, if they are all opened on one page, it will be a torture for both the browser and the viewer.

I believe that every novice learning PHP will have a headache with pagination, but with Momo’s post, you will definitely pat your head and say, hey, it turns out that pagination is so simple? Indeed, please take a deep breath of fresh air now and listen carefully as Silence explains it to you bit by bit.

Suppose we want to process 1000 pieces of data and display 10 pieces on each page. In this case, it will be displayed in 100 pages. Let's first take a look at how to extract 10 pieces of information in mysql.

Select * from table limit 0,10

The above is a very simple mysql query statement. Its function is to extract 10 pieces of data from a table named table and put all The values ​​of the fields are obtained.

The key part is in this section "limit 0,10". The 0 in it is 0 as the starting point, and the following 10 is to display 10 pieces of data, so we need to use 10 as the starting point. How to write the 20th piece of data displayed?

Maybe many people will say “limit 10,20” outright! Oh, this is wrong. The correct way to write it is "limit 10,10". The parameter after it is not the end point but the number to be extracted. Remember.

Now that you know how to extract 10 pieces of data, extracting 1,000 pieces means doing this kind of query 100 times, which means doing the following query:

Limit 0,10                                 Page
Limit 10,10                                                                                                                                                                    . Already? Yes, the first parameter increases by 10 every time the page is turned, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter according to the number of pages, we can display the data in pages. How about it? Is the principle very simple?

But how to change the value of the first parameter according to the number of pages? First, we need to have a page number value, which can be obtained using the GET method of the URL.
For example, index.php?page=18
I believe most of you are familiar with this thing. This kind of URL address can be found everywhere. The function of the page parameter is to pass in the number of pages to be displayed.

Let’s take a look at how it is implemented through a piece of code:






Copy code

The code is as follows:

/*

Author: silently
Date :2006-12-03

*/

$page =isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then the number of pages is 1 .
$ num = 10; // Show 10 data per page

$ db = mysql_connect ("host", "name", "pass"); // Create a database connection
$ select = MySQL_SELECT_DB ("DB", $ DB); // Select the database you want to operate

/*
First of all, how much data we have to get in the database can judge how many pages of the specific pages, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones. The formula is
The total number of data divided by the number of items displayed on each page, and the remainder is rounded to one.
In other words, 10/3=3.3333=4 If there is a remainder, we must round it up by one.
*/<<>
$ Total = MySQL_NUM_ROWS (mysql_query ("Select*from Table"); // The total number of data
$ Pagenum = Ceil ($ Total/$ Num); // Get the total number of pages

//If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
If($page>$pagenum || $page == 0){
Echo "Error: Can Not Found The page.";
Exit;
}

$offset=($page-1)*$num; //Get the first parameter of limit Value, if the first page is (1-1)*10=0, the second page is (2-1)*10=10.

$info=mysql_query("select * from table limit $offset,$num"); //Get the data to be displayed for the corresponding page number
While($it=mysql_fetch_array($info)) {
Echo $it['name']."
";
}                                                     🎜>For($i=1;$i<=$pagenum; $i++){

$show=($i!=$page)?"$i ":"$i";
Echo $show." ";
}

/*Display paging information, if it is the current page, it will be displayed in bold Number, and the remaining page numbers are hyperlinks. If the current page is the third page, it will be displayed as follows
1 2 3 4 5 6
*/
?>


If you read the above code carefully and replace the database connection and query tables with yours, you can see its execution effect.

Isn’t it very simple? Just use your brain to make it display more personalized. Let me give you a little question: how to implement the format of "Home Page, Previous Page, Next Page, Last Page" What about paging?

OK, finish filling the water post and call it a day. ^_^



Copy code
The code is as follows:


/*

Author: Silently
Date :2006-12-03

*/

$page=isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then page The number is 1.
$num=10;                                                                                                                                                                                                                                                                             // Display 10 pieces of data per page >mysql_select_db( "cr_download");                            //Select the database to be operated

/*
First of all, we need to get how much data there is in the database to determine how many pages to divide into. The specific formula is
Total Divide the database by the number of items displayed on each page, and the remainder is one.
That is to say, 10/3=3.3333=4. If there is a remainder, we must round it up by one.
*/

$result=mysql_query("select * from cr_userinfo");
$total=mysql_num_rows($result); //Query all data

$url ='test.php';//Get the URL of this page

//Calculate the page number
$pagenum=ceil($total/$num); 🎜>$page=min($pagenum,$page);//Get the home page
$prepg=$page-1;//Previous page
$nextpg=($page==$pagenum ? 0 : $page+1);//Next page
$offset=($page-1)*$num; *10=0, the second page is (2-1)*10=10.

//Start paging navigation bar code:
$pagenav="Display page ".($total?($offset+1):0)."- ".min($offset+10,$total)." records, total $total records ";


//If there is only one page, jump out Function:
if($pagenum<=1) return false;

$pagenav.=" Homepage ";
if($prepg) $pagenav.=" Previous page "; else $pagenav.=" Previous page ";
if($nextpg) $pagenav.=" Next page "; else $pagenav.=" Next page ";
$ pagenav.=" Last page ";

//Pull down the jump list and loop through all page numbers:
$pagenav.="Go to page page , A total of $ pagenum pages ";

// If the number of pages passed in is greater than the total page number, the error message
if ($ page & gt; $ pagenum) {
Echo" error: Can Not Found The page ".$page;
Exit;
}

$info=mysql_query("select * from cr_userinfo limit $offset,$num"); //Get the corresponding page number The data to be displayed
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "
";
} //Display Data
echo "
";
echo $pagenav;//Output paging navigation

?>


By the way, let’s dig deeper. In practical applications, paging is almost always used when it comes to lists. You can try to make a general paging function, so that this function can be called whenever paging is needed. Haha~~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317716.htmlTechArticleBefore reading this article, please make sure you have mastered some knowledge of PHP and the basics of MYSQL query operations. As a web program, it often has to deal with countless data, such as members...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn