php ajax分页实现程序代码分享

一款基于原生态的php +js +ajax 的分页程序实例,我们详细的从数据库创建到js,php,html页面的创建来告诉你如何实现ajax分页调用数据的方法.

创建数据库代码如下:

  1. CREATE TABLE `tb_user` (
  2. `id` int(10) NOT NULL auto_increment,
  3. `username` varchar(50) NOT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
  6. INSERT INTO `tb_user` VALUES (1, 'aaa');
  7. INSERT INTO `tb_user` VALUES (2, 'bbb');
  8. INSERT INTO `tb_user` VALUES (3, 'ccc');
  9. INSERT INTO `tb_user` VALUES (4, 'ddd');
  10. INSERT INTO `tb_user` VALUES (5, 'eee');
  11. INSERT INTO `tb_user` VALUES (6, 'fff');
  12. INSERT INTO `tb_user` VALUES (7, 'ggg');
  13. INSERT INTO `tb_user` VALUES (8, 'hhh');
  14. INSERT INTO `tb_user` VALUES (9, 'eee');

ajaxpage.js,代码如下:

  1. var http_request=false;
  2. function send_request(url){//初始化,指定处理函数,发送请求的函数
  3. http_request=false;
  4. //开始初始化XMLHttpRequest对象
  5. if(window.XMLHttpRequest){//Mozilla浏览器
  6. http_request=new XMLHttpRequest();
  7. if(http_request.overrideMimeType){//设置MIME类别
  8. http_request.overrideMimeType("text/xml");
  9. }
  10. }
  11. else if(window.ActiveXObject){//IE浏览器
  12. try{
  13. http_request=new ActiveXObject("Msxml2.XMLHttp");
  14. }catch(e){
  15. try{
  16. http_request=new ActiveXobject("Microsoft.XMLHttp");
  17. }catch(e){}
  18. }
  19. }
  20. if(!http_request){//异常,创建对象实例失败
  21. window.alert("创建XMLHttp对象失败!");
  22. return false;
  23. }
  24. http_request.onreadystatechange=processrequest;
  25. //确定发送请求方式,URL,及是否同步执行下段代码
  26. http_request.open("GET",url,true);
  27. http_request.send(null);
  28. }
  29. //处理返回信息的函数
  30. function processrequest(){
  31. if(http_request.readyState==4){//判断对象状态
  32. if(http_request.status==200){//信息已成功返回,开始处理信息
  33. document.getElementById(reobj).innerHTML=http_request.responseText;
  34. }
  35. else{//页面不正常
  36. alert("您所请求的页面不正常!");
  37. }
  38. }
  39. }
  40. function dopage(obj,url){
  41. document.getElementById(obj).innerHTML="正在读取数据...";
  42. reobj = obj;
  43. send_request(url);
  44. }

php调用代码如下:

  1. <title>PHP+ajax分页演示</title>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3. <script language="javascript" src="ajaxpage.js"></script>
  4. <div >
  5. <?php
  6. $terry=mysql_connect("localhost","root","")or die("连接数据库失败:".mysql_error());
  7. mysql_select_db("ajaxtest",$terry);
  8. mysql_query("set NAMES 'utf8'");
  9. $result=mysql_query("select * from tb_user");
  10. $total=mysql_num_rows($result) or die(mysql_error());
  11. $page=isset($_GET['page'])?intval($_GET['page']):1;
  12. $page_size=3;
  13. $url='index.php';
  14. $pagenum=ceil($total/$page_size);
  15. $page=min($pagenum,$page);
  16. $prepage=$page-1;
  17. $nextpage=($page==$pagenum?0:$page+1);
  18. $pageset=($page-1)*$page_size;
  19. $pagenav='';
  20. $pagenav.="显示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录&nbsp;共<b><font color='yellow'>".$total."</font></b>条记录&nbsp;现在是第&nbsp;<b><font color='blue'>".$page."</font></b>&nbsp;页&nbsp;";
  21. if($page<=1)
  22. $pagenav.="<a ;
  23. else
  24. $pagenav.="<a onclick=javascript:dopage('result','$url?page=1') ;
  25. if($prepage)
  26. $pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') ;
  27. else
  28. $pagenav.="<a ;
  29. if($nextpage)
  30. $pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') ;
  31. else
  32. $pagenav.="<a ;
  33. if($pagenum)
  34. $pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') ;
  35. else
  36. $pagenav.="<a ;
  37. $pagenav.="共".$pagenum."页";
  38. if($page>$pagenum){
  39. echo "error:没有此页".$page;
  40. exit();
  41. }
  42. ?>
  43. <table align="center" width="300">
  44. <tr bgcolor="#cccccc" align="center">
  45. <td>用户名</td>
  46. <td>用户密码</td>
  47. </tr>
  48. <?php
  49. $info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
  50. while($array=mysql_fetch_array($info)){
  51. ?>
  52. <tr align="center">
  53. <td><?php echo $array['id'];?></td>
  54. <td><?php echo $array['username'];?></td>
  55. </tr>
  56. <?php
  57. }
  58. ?>
  59. </table>
  60. <?php
  61. echo "<p align=center>$pagenav</p>";
  62. ?>
  63. </div>

大家使用时将上面的代码保存成单个文件就可以实现了.