Ajax PHP paging demonstration

Source: Internet
Author: User

Today, let's look at the pagination in silence. Think about it as if no post has been posted in the original area for a long time. Let's extend it to a PHP + AJAX paging demonstration by the way. Okay, let's talk about it, first, we are still the basic AJAX development framework:
Copy codeThe Code is as follows: 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:
Copy codeThe Code is as follows: <? 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 = dopage ('result', '$ url? Page = 1');> homepage </a> ";
If ($ prepg) $ pagenav. = "<a href = dopage ('result', '$ url? Page = $ prepg ');> previous page </a> "; else $ pagenav. =" Previous page ";
If ($ nextpg) $ pagenav. = "<a href = dopage ('result', '$ url? Page = $ nextpg ');> next page </a> "; else $ pagenav. =" next page ";
$ Pagenav. = "<a href = 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> the key to page turning is to call the dopage () function when page turning, and then update the content in the div using the callback information. Core server code:
Copy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters
$ 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 = dopage ('result', '$ url? Page = 1');> homepage </a> ";
If ($ prepg) $ pagenav. = "<a href = dopage ('result', '$ url? Page = $ prepg ');> previous page </a> "; else $ pagenav. =" Previous page ";
If ($ nextpg) $ pagenav. = "<a href = dopage ('result', '$ url? Page = $ nextpg ');> next page </a> "; else $ pagenav. =" next page ";
$ Pagenav. = "<a href = 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

?>

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.