Home  >  Article  >  Backend Development  >  Brief analysis of PHP+ajax paging examples

Brief analysis of PHP+ajax paging examples

不言
不言Original
2018-04-04 10:30:411185browse

This article mainly introduces the PHP+ajax paging implementation method, and analyzes the related implementation techniques of PHP data query and ajax paging in the form of simple examples. It has certain reference value. Friends in need can refer to it

The example in this article describes the PHP+ajax paging implementation method. Share it with everyone for your reference, the details are as follows:

HTML code is as follows:


<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=&#39;text/javascript&#39;><!--
 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(&#39;result&#39;).innerHTML=http_request.responseText;
   }
   else{
   //页面不正常
   alert("您所请求的页面不正常!");
   }
  }
 }
 function dopage(url)
 {
  document.getElementById(&#39;result&#39;).innerHTML="正在读取数据...";
  send_request(url);
 }
// --></mce:script>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<p id=&#39;result&#39;>
</p>
</body>
</html>


php code is as follows:


header("Content-type: text/html;charset=GBK");//
$link=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);
mysql_select_db(&#39;test&#39;);
mysql_query("SET NAMES &#39;GBK&#39;");
$num=5;//每页显示5条
$sql="select * from article ";
//总共有多少记录
$resultt=mysql_query($sql);
//获取数据库总记录数
$total=mysql_num_rows($result);
echo $total;
//总共有多少页
$pagecount=$total/$num;
//获取当前页
$currentPage=isset($_GET[&#39;page&#39;])?intval($_GET[&#39;page&#39;]):1;
//查询条数
$offset=($currentPage-1)*$num;
//下一页
$nexpage=($currentPage==$pagecount?0:$currentPage+1);
//上一页
$prepg=$currentPage-1;
$url=&#39;ajax.php&#39;;
//根据条数查询信息
$strSQL="select * from article limit $offset,$num";
$pagecontrol="当前第".$currentPage."页" ;
$pagecontrol.="共".$pagecount."页";
$pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("&#39;$url?page=1&#39;)>首 页</a> ";
if($prepg) $pagecontrol.=" <a href="javascript:dopage(" mce_href="javascript:dopage("&#39;$url?page=$prepg&#39;);>上一页</a> "; else $pagecontrol.=" 上一页 ";
if($nexpage) $pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("&#39;$url?page=$nexpage&#39;);>下一页</a>"; else $pagecontrol.="下一页";
$pagecontrol.="<a href="javascript:dopage(" mce_href="javascript:dopage("&#39;$url?page=$pagecount&#39;)>尾 页</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;

Related recommendations:

How to implement paging using ajax combined with Douban search (with code)

How PHP+MySQL implements paging display

How to implement PHP query paging

The above is the detailed content of Brief analysis of PHP+ajax paging examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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