Home > Backend Development > PHP Tutorial > Detailed explanation of PHP+AJAX non-refresh paging implementation code (1/2)_PHP tutorial

Detailed explanation of PHP+AJAX non-refresh paging implementation code (1/2)_PHP tutorial

WBOY
Release: 2016-07-13 16:56:24
Original
808 people have browsed it

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
?>


ajax paging demonstration




$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
Divide the total database by the number of items displayed on each page, and the remainder is rounded to one.
In other words, 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 number calculation

$ Pagenum = CEIL ($ Total/$ Num); // Get the total page number, 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;                                                                                                                                     // 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 records in total";

//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 of $pagenum";

//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 that needs to be displayed for the corresponding page number
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "
";
}                                                                                                                                                                                                                                                                                                   ​  echo"
";
echo $pagenav;//Output paging navigation

?>




1 2

http://www.bkjia.com/PHPjc/631605.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631605.htmlTechArticleDetailed explanation of the PHP+AJAX no-refresh paging implementation code. I was watching an ajax tutorial recently and wanted to write a simple entry-level PHP +AJAX no refresh paging, we copy the code var h according to the ajax development framework code as follows...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template