Talking about the principle of php&mysql paging and its implementation _php example

Source: Internet
Author: User
Before you look at this article, make sure you've mastered some of PHP's knowledge and the basics of MySQL's query operation.

As a Web program, you often have to deal with countless numbers of data, for example, members of the data, the article data, if only dozens of members that is good to do, on a page to show on it, but if your site is thousands of or even hundreds of thousands of members, if all in a page open words whether the browser or viewers are a kind of torture.

I believe that every novice to learn PHP will be page this thing feel very headache, but with the silent this water post, you will certainly pat the head said, hey, the original pagination unexpectedly so simple? Indeed, now please take a breath of fresh air, listen carefully silently give you 1.1 points of decomposition.

Let's say we're dealing with 1000 of data, and we're going to show 10 on each page, and then we'll show you 100 pages, so we'll look at how we can extract 10 messages in MySQL.

Select * FROM table limit 0,10

The above is a very simple MySQL query statement, its role is to extract 10 of data from a table name, and the value of all the fields are obtained.

The key place in this "limit 0,10", which 0 is 0 as the starting point, followed by 10 is to display 10 data, then we want to 10 as the starting point, showing the 20th data how to write it?

Probably a lot of big Congress outspoken say "Limit 10,20"! Ah oh, this can be wrong oh, the correct way to spell is "limit 10,10" the argument behind it is not the end point but the number to extract, remember oh.

Know how to extract 10 data, then extract 1000 is to do 100 times this query Ah, that is to do the following query:

Limit 0,10//First page
Limit 10,10//second page
Limit 20,10//Third page
Limit 30,10//Fourth page
......
Can you see the pattern? Yes, the first parameter increases by 10 on each page, but the second parameter is unchanged.
In other words, we try to change the value of the first parameter according to the number of pages, and then we can display the data in pagination, how is the principle not very simple?

But how do you manage to change the value of the first argument based on the number of pages? First, we want to have a number of pages of the value, with the URL of the get way to obtain.
Like Index.php?page=18.
Believe that most of the big thing is not unfamiliar with it, this URL address is everywhere, where the role of the page parameter is passed in to show the number of pages.

Let's take a piece of code to see how it's done:



Copy Code code as follows:
<?php

/*

Author: Silently
date:2006-12-03

*/

$page =isset ($_get[' page ')? Intval ($_get[' page '): 1; This is to get the value of page in Page=18, if there is no page, then the page number is 1.
$num = 10; Display 10 data per page

$db =mysql_connect ("host", "name", "Pass"); Creating a database connection
$select =mysql_select_db ("db", $db); Select the database to manipulate

/*
First we have to get the database in the end how much data to determine the specific number of pages, the specific formula is
The total number of data divided by the number of bars displayed per page, more than one.
That is to say, 10/3=3.3333=4 will enter one.
*/

$total =mysql_num_rows (mysql_query ("SELECT * from table")); Total number of query data
$pagenum =ceil ($total/$num); Get Total Pages

If the number of pages passed in is greater than the total number of pages, an error message is displayed
If ($page > $pagenum | | $page = = 0) {
Echo "Error:can not Found the page."
Exit;
}

$offset = ($page-1) * $NUM; Gets the value of the first parameter of the limit, if the first page is (1-1) *10=0, and the second page is (2-1) *10=10.

$info =mysql_query ("Select * from table limit $offset, $num"); Get the data that the page needs to display
while ($it =mysql_fetch_array ($info)) {
Echo $it [' name ']. <br/> ";
}//Display data

for ($i =1; $i <= $pagenum; $i + +) {

$show = ($i!= $page)? " <a href= ' index.php?page= '. $i. "' > $i </a> ": <b> $i </b>";
Echo $show. " ";
}

/* Display paging information, if the page is displayed in bold figures, the remaining number of pages is a hyperlink, if the current is the third page is displayed as follows
1 2 3 4 5 6
*/
?>



If you read the code carefully and replace the database connection with the query table, you can see how it works.

is not very simple, as long as the use of brain, you can let it show more personalized Oh, give us a small problem, how to achieve "first prev Next page last" This format of pagination?

OK, the water post is finished. ^_^
Copy Code code as follows:

<?php

/*

Author: Silently
date:2006-12-03

*/

$page =isset ($_get[' page ')? Intval ($_get[' page '): 1; This is to get the value of page in Page=18, if there is no page, then the page number is 1.
$num = 10; Display 10 data per page

$db =mysql_connect ("localhost", "root", "7529639"); Creating a database connection
mysql_select_db ("Cr_download"); Select the database to manipulate

/*
First we have to get the database in the end how much data to determine the specific number of pages, the specific formula is
The total database is divided by the number of bars displayed per page.
That is to say, 10/3=3.3333=4 will enter one.
*/

$result =mysql_query ("SELECT * from Cr_userinfo");
$total =mysql_num_rows ($result); Query all the data

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

Page number calculation
$pagenum =ceil ($total/$num); Get the total number of pages, also the last page
$page =min ($pagenum, $page);//Get home
$PREPG = $page -1;//Previous page
$NEXTPG = ($page = = $pagenum 0: $page + 1);//Next page
$offset = ($page-1) * $NUM; Gets the value of the first parameter of the limit, if the first page is (1-1) *10=0, and the second page is (2-1) *10=10.

To start the pager bar code:
$pagenav = "Show <B>". ($total? ($offset + 1): 0). " </B>-<B> "min ($offset +10, $total)." </B> Records, a total of $total records ";


If only one page jumps out of the function:
if ($pagenum <=1) return false;

$pagenav. = "<a href= ' $url" page=1 ' > Home </a> ";
if ($PREPG) $pagenav. = "<a href= ' $url? page= $prepg ' > front page </a> '; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href= ' $url? page= $nextpg ' > Back page </a> '; else $pagenav. = "Back Page";
$pagenav. = "<a href= ' $url?" page= $pagenum ' > Last </a> ';

Drop-down Jump List, looping through all page numbers:
$pagenav. = "to <select name= ' topage ' size= ' 1 ' onchange= ' window.location=\" $url? page=\ "+this.value ' >\n ';
for ($i =1; $i <= $pagenum; $i + +) {
if ($i = = $page) $pagenav. = "<option value= ' $i ' selected> $i </option>\n";
else $pagenav. = "<option value= ' $i ' > $i </option>\n";
}
$pagenav. = "</select> page, total $pagenum page";

If the number of pages passed in is greater than the total number of pages, an error message is displayed
If ($page > $pagenum) {
Echo "Error:can not Found the page". $page;
Exit;
}

$info =mysql_query ("select * from Cr_userinfo limit $offset, $num"); Get the data that the page needs to display
while ($it =mysql_fetch_array ($info)) {
Echo $it [' username '];
echo "<br>";
}//Display data
echo "<br>";
echo $pagenav//Output paging navigation

?>

By the way, in practical applications, almost all involved in the list will be used to pagination, we can try to do a page general function, so as long as the need to page the place to call this function, hehe ~ ~

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.