Talking about the principle and realization of php@mysql paging in silence
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 php content to clipboard PHP code:
<?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 ID from table"); Total number of query data, ID is an automatically assigned field in the database
$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
$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")
Get the data that you want the page to display, name is a field in the data
while ($rsmysql _fetch_array ($res)) {
Echo $rs [' name ']. <br/> ";
}//Display data
The following is used for, do not say the clatter. There are many other ways to change.
/* 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.
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