Detailed Php+ajax No Refresh paging implementation method _php skills

Source: Internet
Author: User

Php+ajax No Refresh Paging implementation code detailed, recently in the Ajax tutorial, want to write a simple introduction of the Php+ajax No refresh paging, we based on the AJAX development Framework, the code is as follows:

var Http_request=false; function send_request (URL) {//initialization, specifying handler functions, sending requested functions http_request=false;//initiating initialization of XMLHttpRequest object if window.
 XMLHttpRequest) {//mozilla browser http_request=new xmlhttprequest ();
 if (http_request.overridemimetype) {//Set 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, failed to create object instance Window.alert ("Failed to create XMLHTTP Object!")
 ");
return false;
} http_request.onreadystatechange=processrequest;
Determine how to send the request, the URL, and whether to execute the next code http_request.open ("Get", url,true) synchronously;
 Http_request.send (NULL); }//Processing return information function ProcessRequest () {if (http_request.readystate==4) {//Judging object state if (http_request.status==200) {//
 The information was successfully returned and the information document.getElementById (reobj) was processed. Innerhtml=http_request.responsetext; else{//page is not normal alert ("the page you requested is not normal!")
 "); }} function Dopage (obj,url) {document.getElementById (obj). innerhtmL= "reading data ...";
Send_request (URL);
Reobj=obj; }

The content I put in a div shows, when the page action is generated, use Ajax to update div to page effect This is the Content Display page code:

<?php header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK")//output encoding to avoid Chinese garbled?>  

The key to turning the page is to call the Dopage () function, and then use the callback information to update the content in the Div, server-side core code:

<?php header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK")//output encoding to avoid Chinese garbled $page =isset ($_get[' page ')? Intval ($_get['  Page ']): 1;
This is to get the value of page in Page=18, if there is no page, then the page number is 1.          $num = 10;   Each page displays 10 data $db =mysql_connect ("localhost", "root", "7529639");     Create a database connection mysql_select_db ("Cr_download");

Select database to operate/* First we have to get the database in the end how much data to determine the specific number of pages, the specific formula is the total database divided by the number of bars displayed per page, more than one.

That is to say, 10/3=3.3333=4 will enter one.
* * $result =mysql_query ("SELECT * from Cr_userinfo"); $total =mysql_num_rows ($result);         Query all data $url = ' test.php ';//Get page URL//page number calculation $pagenum =ceil ($total/$num); Get the total number of pages, but also the last page $page =min ($pagenum, $page);//get home $PREPG = $page -1;//$NEXTPG = ($page = = $pagenum? 0: $page + 1);/Next Page $          Offset= ($page-1) * $NUM;
Gets the value of the first parameter of the limit, if the first page is (1-1) *10=0, and the second page is (2-1) *10=10. Start the pager bar code: $pagenav = "Show <B>". ($total? ($offset + 1): 0). " </B>-<B> "min ($offset +10, $total)."
</B> Records, a total of $total records ";
If only one page jumps out of the function: if ($pagenum <=1) return false; $pagenav. = "<a href=javascrīpt:dopage" (' Result '), ' $url? page=1 ');> home </a> "; if ($PREPG) $pagenav. = "<a href=javascrīpt:dopage (' result ', ' $url? page= $prepg ');> front page </a>";
else $pagenav. = "front page"; if ($NEXTPG) $pagenav. = "<a href=javascrīpt:dopage (' result ', ' $url? page= $nextpg ');> back page </a>";
else $pagenav. = "Back Page";
$pagenav. = "<a href=javascrīpt:dopage (' result ', ' $url? page= $pagenum ');> last </a>";
$pagenav. = "</select> page, total $pagenum page";
 If the number of page parameters passed in is greater than the total number of pages, an error message if ($page > $pagenum) {Echo Error:can not Found the page is displayed. $page;
Exit; $info =mysql_query ("select * from Cr_userinfo limit $offset, $num");
 Gets the data that is required to display the corresponding number of pages while ($it =mysql_fetch_array ($info)) {Echo $it [' username '];
echo "<br>";
 ///Display data echo "<br>";
 echo $pagenav//Output Paging navigation?>

Above for you to explain in detail the Php+ajax no refresh paging implementation method, I hope to learn PHP program design help.

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.