温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP+ajax如何实现分页

发布时间:2021-09-05 18:44:37 来源:亿速云 阅读:180 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关PHP+ajax如何实现分页的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体如下:

HTML代码如下:

<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>title</title>
<mce:script type='text/javascript'><!--
 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.open("GET",url,true);
  http_request.onreadystatechange=processrequest;
  //确定发送请求方式,URL,及是否同步执行下段代码
  http_request.send(null);
 }
 //处理返回信息的函数
 function processrequest(){
  if(http_request.readyState==4){//判断对象状态
   if(http_request.status==200){//信息已成功返回,开始处理信息
   document.getElementById('result').innerHTML=http_request.responseText;
   }
   else{
   //页面不正常
   alert("您所请求的页面不正常!");
   }
  }
 }
 function dopage(url)
 {
  document.getElementById('result').innerHTML="正在读取数据...";
  send_request(url);
 }
// --></mce:script>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<div id='result'>
</div>
</body>
</html>

php代码如下:

header("Content-type: text/html;charset=GBK");//
$link=mysql_connect('localhost','root','root');
mysql_select_db('test');
mysql_query("SET NAMES 'GBK'");
$num=5;//每页显示5条
$sql="select * from article ";
//总共有多少记录
$resultt=mysql_query($sql);
//获取数据库总记录数
$total=mysql_num_rows($result);
echo $total;
//总共有多少页
$pagecount=$total/$num;
//获取当前页
$currentPage=isset($_GET['page'])?intval($_GET['page']):1;
//查询条数
$offset=($currentPage-1)*$num;
//下一页
$nexpage=($currentPage==$pagecount?0:$currentPage+1);
//上一页
$prepg=$currentPage-1;
$url='ajax.php';
//根据条数查询信息
$strSQL="select * from article limit $offset,$num";
$pagecontrol="当前第".$currentPage."页" ;
$pagecontrol.="共".$pagecount."页";
$pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("'$url?page=1')>首 页</a> ";
if($prepg) $pagecontrol.=" <a href="javascript:dopage(" mce_href="javascript:dopage("'$url?page=$prepg');>上一页</a> "; else $pagecontrol.=" 上一页 ";
if($nexpage) $pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("'$url?page=$nexpage');>下一页</a>"; else $pagecontrol.="下一页";
$pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("'$url?page=$pagecount')>尾 页</a>";
$result1=mysql_query($strSQL);
echo "<table border=1 width=100%>";
echo "<tr>";
echo "<td>编 号</td><td>标 题</td><td>新闻类型</td>";
echo "</tr>";
while($row=mysql_fetch_array($result1))
{
  echo "<tr>";
  echo "<td>$row[id]</td><td>$row[title]</td><td>$row[classname]</td>";
  echo "</tr>";
}
echo "</table>";
echo "<br/>";
echo $pagecontrol;

感谢各位的阅读!关于“PHP+ajax如何实现分页”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI