Quietly talking about the principle of php@mysql paging and its realization _php tutorial

Source: Internet
Author: User
Tags learn php

Talking about the principle and realization of php&mysql paging in silence

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

As a web program, often to deal with countless data, such as the member's data, the article data, if only dozens of members that very good, on a page display on it, but if your site is thousands of or even hundreds of thousands of members, if all on a page open, whether it is a browser or viewer is a torture.

I believe that every novice to learn PHP will be a page this thing feel very headache, but with the silent this water, you will certainly pat the head said, hey, the original paging is so simple? Indeed, now please take a deep breath of fresh air and listen carefully to your 1.1-point decomposition.

Let's say we're going to work on 1000 data, we're going to show 10 on each page, and that's going to be 100 pages, so let's take a look at how to extract 10 messages in MySQL.

Select * FROM table limit 0,10

It's a simple MySQL query that extracts 10 of data from a table called tables and gets all the values of the fields.

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

Probably a lot of big Congress outspoken say "Limit 10,20"! Oh, that's wrong. Oh, the correct way to do this 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 Yes, 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
See what the rules are? Yes, the first parameter increases by 10 per 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, we can display the data in pagination, how is the principle simple?

But how do you manage to change the value of the first parameter based on the number of pages? First, we need to have a value for the number of pages, which is obtained by the URL's Get method.
Like Index.php?page=18.
Believe most of this thing is not strange, this URL address is ubiquitous, where the role of the page parameter is to pass in the number of pages to be displayed.

Let's take a look at a piece of code to see how it's done:
Copy php content to clipboard PHP code:


Author: Silently


$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 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 to manipulate

First we want to get the database in the end how much data in order 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 is more than one.
That is to say, 10/3=3.3333=4 will be in 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 needed to display the page number, name is a field in the data
while ($rsmysql _fetch_array ($res)) {
Echo $rs [' name ']. "
}//Display data

The following is used for, do not say the DA. There are many other ways to change.

/* Display paging information, if the page is displayed in bold numbers, the remaining pages are hyperlinks, if the current third page is displayed as follows
1 2 3 4 5 6

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

http://www.bkjia.com/PHPjc/631898.html www.bkjia.com true http://www.bkjia.com/PHPjc/631898.html techarticle talking about the principle of phpmysql paging and its implementation before looking at this article, make sure you have mastered some knowledge of PHP and the basics of MySQL query operations. As a web program, often and not ...

  • 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.