PHP paging implementation

Source: Internet
Author: User
The implementation result is shown in the figure below:

The code is not described in detail. The comment contains 1 <? PHP
2 // Page Current page, number of pages per page of num
3 $ page = isset ($ _ Get ['page'])? Intval ($ _ Get ['page']): 1;
4 $ num = 5;
5
6 $ conn = mysql_connect ('127. 0.0.1 ', 'root ','');
7 if (! $ Conn ){
8 die ('could not connect: '. mysql_error ());
9}
10 mysql_select_db ('shop ');
11 // obtain the total number of records
12 $ Total = mysql_num_rows (mysql_query ("select * from user "));
13 // calculate the page number
14 $ pagenum = Ceil ($ total/$ num );
15 // the maximum number of pages cannot be exceeded.
16 if ($ page> $ pagenum | $ page = 0 ){
17 echo 'error: Can not found the page .';
18 exit;
19}
20 // the statement "select * from Table limit" extracts ten pieces of information from the table. 0 indicates the start point and 10 indicates the number of extracted items.
21 // $ offset is the start value. For example, if the current page is the first page and there are 5 entries on each page, the start point is 0. Similarly, if the current page is the second page, the start point is 5.
22 $ offset = ($ page-1) * $ num;
23 $ result = mysql_query ("select * from user limit $ offset, $ num ");
24 while ($ it = mysql_fetch_array ($ result )){
25 echo 'id: '. $ it ['id']. 'name:'. $ it ['name']. '<br/> ';
26}
27 // display link page switch link, no link on the current page
28 For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
29 $ show = ($ I! = $ Page )? "<A href = 'index. php? Page = ". $ I." '> $ I </a> ":" <B> $ I </B> ";
30 echo $ show ."";
31}
32 echo $ total. 'records, 5 records per page, total '. $ pagenum.' page ';
33 mysql_free_result ($ result );
34 mysql_close ($ conn );
35?>

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.