Home > Article > Backend Development > PHP Pagination Navigation Ajax PHP Pagination Demonstration
Today I was watching Momo’s explanation of paging. It seemed like no one had posted in the original area for a long time, so I just expanded Momo’s one and gave a PHP+AJAX paging demonstration. Okay, that’s it. First of all, we are still basic. AJAX development framework:
Copy code The code is as follows:
var http_request=false;
function send_request(url){//Initialization, specify processing function, function to send request
http_request=false;
//Start Initialize the XMLHttpRequest object
if(window. rideMimeType("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.
//Determine the request method, URL, and whether to execute the next code synchronously
http_request.open("GET",url,true);
http_request.send(null);
}
//Processing Function that returns information
function processrequest(){
if(http_request.readyState==4){//Determine the object status
if(http_request.status==200){//The information has been returned successfully, start processing the information
document. getElementById(reobj).innerHTML=http_request.responseText;
} using. ");
}
}
}
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, I use AJAX to update the DIV to achieve the page turning effect. This is the content display page code:
header("Content-type: text/html;charset=GBK");//输出编码,避免中文乱码
?>
Copy code The code is as follows:
header("Content-type: text/html;charset=GBK");//Output encoding to avoid Chinese garbled characters
$ page=isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then the number of pages is 1.
$num=10; elect_db("cr_download"); //Select to Database of operation
/*
First of all, we need to get how much data there is in the database to determine how many pages it needs to be divided into. The specific formula is
The total database is divided by the number of items displayed on each page, and the remainder is rounded up to one.
That is to say, 10/3=3.3333=4 If there is a remainder, we must round it up by 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 code calculation p $ Pagenum = CEIL ($ Total/$ Num); // Get the total page number, which is the last page
$ page = min ($ pagenum, $ page); // gets 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="Display page ".($total?($offset+1):0)."-".min ($offset+10,$total)." records, total $total records ";
//If there is only one page, jump out of the function:
if($pagenum<=1) return false;
$pagenav.=" Homepage ";
if($prepg) $pagenav.=" Previous page "; else $pagenav.=" Previous page ";
if($nextpg) $pagenav.= " Next page "; else $pagenav.=" Next page ";
$pagenav.=" Last page ";
$pagenav.=" Pages, total $pagenum pages ";
//If the page number parameter passed in is greater than the total number of pages, an error message will be 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 to be displayed for the corresponding page number
While($it=mysql_fetch_array($info)){
Echo $it[ 'username'];
echo "
";
} echo"
echo $pagenav;//Output paging navigation
?>
The above has introduced the PHP paging navigation Ajax PHP paging demonstration, including the content of PHP paging navigation. I hope it will be helpful to friends who are interested in PHP tutorials.