Ajax paging code

Source: Internet
Author: User
Tags min

<? Php
Header ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters
?>
<Html>
<Head>
<Title> ajax paging demonstration </title>
<Script language = "javascript" src = "ajaxpg. js"> </script>
</Head>
<Body>
<Div id = "result">
<? Php
$ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. If there is no page, the page number is 1.
$ Num = 10; // 10 data entries per page

$ Db = mysql_connect ("localhost", "root", "7529639"); // create a database connection
Mysql_select_db ("cr_download"); // select the database to operate

/*
First, we need to obtain the actual amount of data in the database to determine the specific number of pages to be divided. The specific formula is
Total databases divided by the number of entries displayed on each page, more than one.
That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter.
*/

$ Result = mysql_query ("select * from cr_userinfo ");
$ Total = mysql_num_rows ($ result); // query all data

$ Url = 'Test. Php'; // Obtain the URL of this page

// Page number calculation
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages, which is also the last page.
$ Page = min ($ pagenum, $ page); // Get the homepage
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ pagenum? 0: $ page + 1); // Next page
$ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. If the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.

// Start the paging navigation bar code:
$ Pagenav = "show the <B>". ($ total? ($ Offset + 1): 0 ). "</B>-<B> ". min ($ offset + 10, $ total ). "</B> records, total $ total records ";

// If there is only one page, the function will jump out:
If ($ pagenum <= 1) return false;

$ Pagenav. = "<a href = javascript: dopage ('result', '$ url? Page = 1');> homepage </a> ";
If ($ prepg) $ pagenav. = "<a href = javascript: dopage ('result', '$ url? Page = $ prepg ');> Previous page </a> "; else $ pagenav. =" previous page ";
If ($ nextpg) $ pagenav. = "<a href = javascript: dopage ('result', '$ url? Page = $ nextpg ');> Next page </a> "; else $ pagenav. =" next page ";
$ Pagenav. = "<a href = javascript: dopage ('result', '$ url? Page = $ pagenum ');> last page </a> ";
$ Pagenav. = "</select> Page, total $ pagenum page ";

// If the input page number parameter is greater than the total page number, the 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"); // obtain the data to be displayed on the corresponding page
While ($ it = mysql_fetch_array ($ info )){
Echo $ it ['username'];
Echo "<br> ";
} // Display Data
Echo "<br> ";
Echo $ pagenav; // output paging navigation

?>
</Div>
</Body>
</Html>

Related Article

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.