php ajax分页实现程序代码分享
一款基于原生态的php +js +ajax 的分页程序实例,我们详细的从数据库创建到js,php,html页面的创建来告诉你如何实现ajax分页调用数据的方法.
创建数据库代码如下:
- 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 ;
-
-
- 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, 'eee');
ajaxpage.js,代码如下:
- var http_request=false;
- function send_request(url){
- http_request=false;
-
- if(window.XMLHttpRequest){
- http_request=new XMLHttpRequest();
- if(http_request.overrideMimeType){
- http_request.overrideMimeType("text/xml");
- }
- }
- else if(window.ActiveXObject){
- 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;
-
- 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>
大家使用时将上面的代码保存成单个文件就可以实现了.