php+ajax实现无刷新分页的方法
程序员文章站
2023-01-19 08:25:28
本文实例讲述了php+ajax实现无刷新分页的方法。分享给大家供大家参考。具体实现方法如下:
这是一款基于原生态的php +js +ajax 的分页程序实例,我们详细的从...
本文实例讲述了php+ajax实现无刷新分页的方法。分享给大家供大家参考。具体实现方法如下:
这是一款基于原生态的php +js +ajax 的分页程序实例,我们详细的从数据库创建到js,php,html页面的创建来告诉你如何实现ajax分页调用数据的方法。
具体步骤如下:
一、创建数据库
sql语句如下:
复制代码 代码如下:
create table `tb_user` (
`id` int(10) not null auto_increment,
`username` varchar(50) not null,
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=10 ;
`id` int(10) not null auto_increment,
`username` varchar(50) not null,
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=10 ;
insert into `tb_user` values (1, 'aaa');
insert into `tb_user` values (2, 'bbb');
insert into `tb_user` values (3, 'ccc');
insert into `tb_user` values (4, 'ddd');
insert into `tb_user` values (5, 'eee');
insert into `tb_user` values (6, 'fff');
insert into `tb_user` values (7, 'ggg');
insert into `tb_user` values (8, 'hhh');
insert into `tb_user` values (9, '����');
二、ajaxpage.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="正在读取数据...";
reobj = obj;
send_request(url);
}
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="正在读取数据...";
reobj = obj;
send_request(url);
}
三、php调用代码如下:
复制代码 代码如下:
<title>php+ajax分页演示</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script language="javascript" src="ajaxpage.js"></script>
<div id="result">
<?php
$terry=mysql_connect("localhost","root","")or die("连接数据库失败:".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("set names 'utf8'");
$result=mysql_query("select * from tb_user");
$total=mysql_num_rows($result) or die(mysql_error());
$page=isset($_get['page'])?intval($_get['page']):1;
$page_size=3;
$url='index.php';
$pagenum=ceil($total/$page_size);
$page=min($pagenum,$page);
$prepage=$page-1;
$nextpage=($page==$pagenum?0:$page+1);
$pageset=($page-1)*$page_size;
$pagenav='';
$pagenav.="显示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录 共<b><font color='yellow'>".$total."</font></b>条记录 现在是第 <b><font color='blue'>".$page."</font></b> 页 ";
if($page<=1)
$pagenav.="<a style=cursor:not-allowed;>首页</a> ";
else
$pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首页</a> ";
if($prepage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>上一页</a> ";
if($nextpage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>下一页</a> ";
if($pagenum)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>尾页</a> ";
$pagenav.="共".$pagenum."页";
if($page>$pagenum){
echo "error:没有此页".$page;
exit();
}
?>
<table align="center" border="2" width="300">
<tr bgcolor="#cccccc" align="center">
<td>用户名</td>
<td>用户密码</td>
</tr>
<?php
$info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
<tr align="center">
<td><?php echo $array['id'];?></td>
<td><?php echo $array['username'];?></td>
</tr>
<?php
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>";
?>
</div>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script language="javascript" src="ajaxpage.js"></script>
<div id="result">
<?php
$terry=mysql_connect("localhost","root","")or die("连接数据库失败:".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("set names 'utf8'");
$result=mysql_query("select * from tb_user");
$total=mysql_num_rows($result) or die(mysql_error());
$page=isset($_get['page'])?intval($_get['page']):1;
$page_size=3;
$url='index.php';
$pagenum=ceil($total/$page_size);
$page=min($pagenum,$page);
$prepage=$page-1;
$nextpage=($page==$pagenum?0:$page+1);
$pageset=($page-1)*$page_size;
$pagenav='';
$pagenav.="显示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录 共<b><font color='yellow'>".$total."</font></b>条记录 现在是第 <b><font color='blue'>".$page."</font></b> 页 ";
if($page<=1)
$pagenav.="<a style=cursor:not-allowed;>首页</a> ";
else
$pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首页</a> ";
if($prepage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>上一页</a> ";
if($nextpage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>下一页</a> ";
if($pagenum)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>尾页</a> ";
$pagenav.="共".$pagenum."页";
if($page>$pagenum){
echo "error:没有此页".$page;
exit();
}
?>
<table align="center" border="2" width="300">
<tr bgcolor="#cccccc" align="center">
<td>用户名</td>
<td>用户密码</td>
</tr>
<?php
$info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
<tr align="center">
<td><?php echo $array['id'];?></td>
<td><?php echo $array['username'];?></td>
</tr>
<?php
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>";
?>
</div>
希望本文所述对大家的php程序设计有所帮助。