Detailed explanation of the PHP+AJAX non-refresh paging implementation code. I was watching an ajax tutorial recently and wanted to write a simple entry-level PHP+AJAX non-refresh paging. We based on the ajax development framework
代码如下 | 复制代码 |
var http_request=false; function send_request(url){//初始化,指定处理函数,发送请求的函数 http_request=false; //开始初始化XMLHttpRequest对象 if(window.XMLHttpRequest){//Mozilla浏览器 http_request=new XMLHttpRequest(); if(http_request.overrideMimeType){//设置MIME类别 http_request.overrideMimeType("text/xml"); } } else if(window.ActiveXObject){//IE浏览器 try{ http_request=new ActiveXObject("Msxml2.XMLHttp"); }catch(e){ try{ http_request=new ActiveXobject("Microsoft.XMLHttp"); }catch(e){} } } if(!http_request){//异常,创建对象实例失败 window.alert("创建XMLHttp对象失败!"); return false; } http_request.onreadystatechange=processrequest; //确定发送请求方式,URL,及是否同步执行下段代码 http_request.open("GET",url,true); http_request.send(null); } //处理返回信息的函数 function processrequest(){ if(http_request.readyState==4){//判断对象状态 if(http_request.status==200){//信息已成功返回,开始处理信息 document.getElementById(reobj).innerHTML=http_request.responseText; } else{//页面不正常 alert("您所请求的页面不正常!"); } } } function dopage(obj,url){ document.getElementById(obj).innerHTML="正在读取数据..."; 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:
Code:
The code is as follows | Copy code |
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 That's 1. $num=10; $db=mysql_connect("localhost","root","7529639"); //Create database connection
mysql_select_db("cr_download"); //Select the database to operate
First, 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
$total=mysql_num_rows($result); //Query all data //Page number calculation
$ Pagenum = CEIL ($ Total/$ Num); // Get the total page number, which is also the last page
$pagenav="Display page ".($total?($offset+1):0)."-".min($offset+10,$total). " records, $total records in total";
if($pagenum<=1) return false;
if($prepg) $pagenav.=" Previous page "; else $pagenav.= "Previous page";
If($page>$pagenum){ $info=mysql_query("select * from cr_userinfo limit $offset,$num"); //Get the data that needs to be displayed for the corresponding page number
|