The principle and implementation of PHP from zero single row (14) data pagination display

Source: Internet
Author: User

Pagination display is one of the most frequently handled aspects of Web programming. The so-called paging display, is the result set by the program to display a paragraph. For paginated display, two initial parameters are required: How many records are displayed per page and what page is currently. With the complete result set, the pagination of the data can be displayed. For other functions, such as previous page, next page, etc. can be processed according to the above information.

To get the first 10 records in a table, you can use the following SQL statement:

SELECT * from A_table LIMIT 0,10

To find the 11th to 20th records, use the following SQL statement:

SELECT * from A_table LIMIT 10,10

To find the 21st to 30th Record, the SQL statement used is as follows:

SELECT * from A_table LIMIT 20,10

The above SQL statement can be seen, each fetch 10 records, equivalent to display 10 data per page, and each time you want to get the beginning of the record and the number of pages between the time there is a relationship between: start position = (Current page-1) * Each page to display the Count of records. If the variable $page_size represents the number of records displayed per page, and the variable $cur_page represents the current page, the above can be summarized using the SQL statement template shown below:

SELECT * FROM table limit ($cur _page-1) * $page _size, $page _size;

In this way, you get the SQL statement that gets the data in the paging case. Where $page_size can be set according to the actual situation as a fixed value, the actual development, the current page $cur_page can be passed by the parameters. In addition, the total number of pages that the data will display can be calculated between the total records and the number of records displayed per page. For example, if the total number of records divided by the number of records displayed per page, there is no remainder, then the total page count is the quotient of the two.

<?php$host= ' localhost '; $user _name= ' root '; $password = ' HelloWorld '; $conn =mysql_connect ($host, $user _name,$ password); if (! $conn) {die (' fail! '). Mysql_error ());} mysql_select_db (' Test '), if (Isset ($_get[' page ')) {$page =$_get[' page '];} else{$page = 1;} $page _size=2; $sql = ' select * from users '; $result =mysql_query ($sql); $total =mysql_num_rows ($result); if ($total) {if ($ total< $page _size) $page _count=1;if ($total% $page _size) {$page _count= (int) ($total/$page _size) +1;} else{$page _count= $total/$page _size;}} else{$page _count=0;} $turn _page= ", if ($page ==1) {$turn _page.= ' Index | Before | ';} else{$turn _page.= ' <a href=13-8.php?page=1>index</a> | <a href=13-8.php?page= '. ( $page-1). ' >Before</a> | ';} if ($page = = $page _count | | $page _count==0) {$turn _page.= ' Next | Last ';} else{$turn _page.= ' <a href=13-8.php?page= '. ( $page + 1). ' > Next </a> | <a href=13-8.php?page= '. $page _count. ' > Last </a> ';} $sql = ' Select Id,name,sex,age from users limit '. ($page-1) * $page _size. ', '. $page _size; $result =mysql_qUery ($sql) OR die ("<br/>ERROR:<b>". Mysql_error (). " </b><br/>sql: ". $sql);? >
**********************

Post get, is the two ways to submit a form, get pass value is obtained by $_get, POST submission form is used $_post
The difference between post and get is that one displays the parameter in the Address bar and the other does not display

For example, if you log in by using get mode, then your value will be displayed on the address bar, so there is no security to say
And when you search or have page numbers, use post to hide the parameters in the Address bar, which makes no sense.

With $_get, you can get the value of the parameter on the browser's address bar (a string of characters after the question mark), such as www.baidu.com/s?wd=123, then you can use $_get to get the parameters (which you can understand as events, actions, or parameters, This value is consistent with the name of input when the form is passed and the value of WD is 123, and multiple parameters are concatenated with the &, for example, An=0&si=5 is understood as the value of the An parameter is 0 and the value of the SI parameter is 5.

**********************

For example, you enter an address called www.iron-feet.cn/?page=2.
$_get["Page"] is the value of this page in the address, that is, to get 2

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.