PHP + AJAX implementation code for refreshing pagination (1/2)

Source: Internet
Author: User
The code is as follows: Copy code
Var http_request = false;
Function send_request (url) {// initialization, specifying the processing function and sending the request function
Http_request = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets the MIME category
Http_request.overrideMimeType ("text/xml ");
    }
}
Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHttp ");
} Catch (e ){
Try {
Http_request = new ActiveXobject ("Microsoft. XMLHttp ");
} Catch (e ){}
    }
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. alert ("An error occurred while creating the XMLHttp object! ");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method, URL, and whether to execute the following code synchronously
Http_request.open ("GET", url, true );
Http_request.send (null );
   }
// Function for processing the returned information
Function processrequest (){
If (http_request.readyState = 4) {// judge the object status
If (http_request.status = 200) {// The message is returned successfully. Start to process the information.
Document. getElementById (reobj). innerHTML = http_request.responseText;
    }
Else {// The page is abnormal.
Alert ("the page you requested is abnormal! ");
    }
}
   }
Function dopage (obj, url ){
Document. getElementById (obj). innerHTML = "reading data ...";
Send_request (url );
Reobj = obj;
}
 

The content is displayed in a div. When the page turning action is generated, update the DIV using AJAX to achieve the page turning effect. This is the page code for displaying the content:

Code:

The code is as follows: Copy code

<? Php
Header ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters
?>
<Html>
<Head>
<Title> ajax paging demonstration </title>
<Scr ī pt language = "javascr ī pt" src = "ajaxpg. js"> </scr ī pt>
</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 = Invalid Cr partition pt: dopage ('result', '$ url? Page = 1');> homepage </a> ";
If ($ prepg) $ pagenav. = "<a href = distinct Cr partition pt: dopage ('result', '$ url? Page = $ prepg ');> Previous page </a> "; else $ pagenav. =" previous page ";
If ($ nextpg) $ pagenav. = "<a href = distinct Cr partition pt: dopage ('result', '$ url? Page = $ nextpg ');> Next page </a> "; else $ pagenav. =" next page ";
$ Pagenav. = "<a href = Invalid Cr partition pt: 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>

Homepage 1 2 Last page

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.