Home>

php + ajax non-refresh paging implementation code in detail,Recently after watching the ajax tutorial, I just want to write a simple introduction to php + ajax without refreshing pagination,We develop framework based on ajax,code show as below:

var http_request=false;
function send_request (url) {//Initialization, specify the processing function,Send Request Function
http_request=false;
//Start to initialize the 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) {//iebrowser
 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 the sending request method,url, and whether to execute the next code synchronously
http_request.open ("get", url, true);
http_request.send (null);
 }
 //functions that return information
 function processrequest () {
if (http_request.readystate == 4) {//Judgment object state
 if (http_request.status == 200) {//The information has been successfully returned,Start processing 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;
}

I display the content in a div. When the page turning action occurs,Use ajax to update the div to achieve page turning effect. This is the content display page code:

<?php
header ("content-type:text/html;charset=gbk");//output encoding,Avoid garbled Chinese
?>
<html>
<head>
<title>ajax pagination demo</title>
<scrīpt language="javascrīpt" src="ajaxpg.js"</scrīpt>
</head>
<body>
<div>
<?php
$page=isset ($_ get ["page"])?intval ($_ get ["page"]):1;//This sentence is to get the value of the page in page=18. If there is no page, Then the number of pages is 1.
$num=10;//Display 10 pieces of data per page
$db=mysql_connect ("localhost", "root", "7529639");//Create a database connection
mysql_select_db ("cr_download");//Select the database to operate
/*
First of all we have to get how much data is in the database,To determine how many pages i want to divide,The specific formula is
Divide the total database by the number of bars displayed per page,I have more than one.
In other words, 10/3=3.3333=4 has a remainder and it is necessary to advance one.
* /
$result=mysql_query ("select * from cr_userinfo");
$total=mysql_num_rows ($result);//Query all data
$url="test.php";//Get the URL of this page
//Page number calculation
$pagenum=ceil ($total/$num);//Get the total number of pages,And 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;//Get 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 paging navigation bar code:
$pagenav="Show the first<b>". ($total?($offset + 1):0). "</b>-<b>". min ($offset + 10, $total ). "</b>records of $total records";
//If there is only one page, jump 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 ");>Previous page</a>";else $pagenav .="Previous page";
if ($nextpg) $pagenav.="<a href=javascrīpt:dopage (" result "," $url?page=$nextpg ");>Next page</a>";else $pagenav .="Next page";
$pagenav.="<a href=javascrīpt:dopage (" result "," $url?page=$pagenum ");>last page</a>";
$pagenav.="</select>pages of $pagenum pages";
//If the number of pages passed in parameter is greater than the total number of pages,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");//Get the data needed 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 page navigation
?>
</div>
</body>
</html>

The key to turning the page is to call the dopage () function when turning the page, 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,Avoid garbled Chinese
$page=isset ($_ get ["page"])?intval ($_ get ["page"]):1;//This sentence is to get the value of the page in page=18. If there is no page, Then the number of pages is 1.
$num=10;//Display 10 pieces of data per page
$db=mysql_connect ("localhost", "root", "7529639");//Create a database connection
mysql_select_db ("cr_download");//Select the database to operate
/*
First of all we have to get how much data is in the database,To determine how many pages i want to divide,The specific formula is
Divide the total database by the number of bars displayed per page,I have more than one.
In other words, 10/3=3.3333=4 has a remainder and it is necessary to advance one.
* /
$result=mysql_query ("select * from cr_userinfo");
$total=mysql_num_rows ($result);//Query all data
$url="test.php";//Get the URL of this page
//Page number calculation
$pagenum=ceil ($total/$num);//Get the total number of pages,And 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;//Get 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 paging navigation bar code:
$pagenav="Show the first<b>". ($total?($offset + 1):0). "</b>-<b>". min ($offset + 10, $total ). "</b>records of $total records";
//If there is only one page, jump 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 ");>Previous page</a>";else $pagenav .="Previous page";
if ($nextpg) $pagenav.="<a href=javascrīpt:dopage (" result "," $url?page=$nextpg ");>Next page</a>";else $pagenav .="Next page";
$pagenav.="<a href=javascrīpt:dopage (" result "," $url?page=$pagenum ");>last page</a>";
$pagenav.="</select>pages of $pagenum pages";
//If the number of pages passed in parameter is greater than the total number of pages,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");//Get the data needed 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 page navigation
 ?>
php
  • Previous UIActivityViewController called in iOS9 system sharing
  • Next JavaScript implements Countdown snippet Item1 (very practical)