php ajax 分页--utf-8
好的东东大家一起分享,今天弄了个ajax分页.
挺简单的,主要是我们要理解ajax的工作原理.
page.php
<title>ajax分页演示</title>
<script language="javascript" src="ajaxpg.js"></script>
<div id="result">
<?php
//获取get传过来的值
$page=isset($_GET['page'])?intval($_GET['page']):1;
$num=10;
//数据库连接
$db=mysql_connect("localhost","root","fkedwgwy");
mysql_select_db("testerp");
//解决乱码问题
mysql_query("SET NAMES 'utf-8'");
//查询获取最大记录数
$result=mysql_query("select * from bookinfo");
$total=mysql_num_rows($result)or die(mysql_error());
$url='test.php';
$pagenum=ceil($total/$num);
$page=min($pagenum,$page);
$prepg=$page-1;
$nextpg=($page==$pagenum ? 0 : $page+1);
$offset=($page-1)*$num;
$pagenav="显示第 <B>".($total?($offset+1):0)."</B>-<B>".min($offset+10,$total)."</B> 条记录,共 $total 条记录 ";
if($pagenum<=1) return false;
$pagenav.=" <a href=javascript:dopage('result','$url?page=1');>首页</a> ";
if($prepg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$prepg');>前页</a> "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$nextpg');>后页</a> "; else $pagenav.=" 后页 ";
$pagenav.=" <a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a> ";
$pagenav.="</select> 页,共 $pagenum 页";
//错误信息
If($page>$pagenum){
Echo "Error : Can Not Found The page ".$page;
Exit;
}
//获取所需要显示的数据
$info=mysql_query("select * from bookinfo limit $offset,$num");
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "<br>";
}
//显示数据
echo"<br>";
echo $pagenav;//输出分页导航
?>
</div>
ajax.js
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;
}
这就是实例.您可以到这里下载实例
http://download.csdn.net/source/473512
-----------------------------------------------------------------------------------------技术交流群:37304662
本群主要交流技术为:PHP,CGI,SQL,MYSQL等Web编程相关技术、数据库.