ajax完美实现两个网页 分页功能的实例代码

时间:2023-11-14 12:57:27 

分页的首页

<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<h1 align="center">武侠小说分页</h>
<br/>
<script src="jquery-1.4.2.min.js"></script>
<script>
var page='';
  function init(page){
  document.getElementById("tables").innerHTML='';
   var xhr;
   if(window.XMLHttpRequest){
 xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
 xhr =new ActiveXObject("Microsoft.XMLHTTP")
}
var url="fenye.php";
 xhr.open("POST",url,true);
 xhr.onreadystatechange=callback;
 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 if(page){
 xhr.send("page="+page);
 }
 else {xhr.send("page=1");}
 var content;
 function callback(){
   if(xhr.readyState==4){
     if(xhr.status==200){
      var json =eval('('+xhr.responseText+')');
   //alert(xhr.responseText);
  var fenye=json.str;
 //  alert(fenye);
     document.getElementById('div').innerHTML=fenye;
  content="<th>ID</th><th>名称</th><th>作者</th><th>出版社</th><th>ISBN号</th><th>类型</th><th>价格</th>";
  for(var i=0;i<json.info.length;i++){
  content+="<tr><td>"+json.info[i].id+"</td><td>"+json.info[i].name+"</td><td>"+json.info[i].author+"</td><td>"+json.info[i].publisher+"</td><td>"+json.info[i].isbn+"</td><td>"+json.info[i].type+"</td><td>"+json.info[i].price+"</tr>";
  document.getElementById("tables").innerHTML=content;
  }
    // alert(fenye);
     }
   }
   }
 }
</script>


<body onLoad="init()">
 <h3 align="center">jquery实现$.ajax的分页</h3>
<table id="tables" style=" width:500px; height:100px; text-align:center" align="center" border="5" bordercolor="#FF6600">
<th>ID</th><th>名称</th><th>作者</th><th>出版社</th><th>ISBN号</th><th>类型</th><th>价格</th>
</table>
<table align="center" style="margin-top:15px">
 <tr><td>
 <td style="colspan:3;height:20">
 <div id="div" style="position:absolute; left: 447px; top: 218px; width: 411px; height: 22px;"></div>
 </td>
</tr>
</table>
<br />
</body>
 

分页的php精华代码

<?php
    //命令模型层处理数据
  $link=mysql_connect('localhost','root','') or die("失败");
  mysql_select_db('books',$link) or die("连接数据库出错了!");
  //每页显示记录数
  $pagesize = 2;
  //求出总的记录数
  $sql = "select count(*) as total_rows from books";
  $result = mysql_query($sql);
  $total_rows = mysql_fetch_array($result);
  //求总共的页码数
  $pages = ceil($total_rows[0]/$pagesize);
  //当前第几页
  $page = $_POST['page'];
  $strtext = "当前第".$page."页"."总共".$pages."页"."共".$total_rows[0]."记录";
  //var_dump($str);
  //接下来,我要根据当前点击的页码求出对应的数据
  $offset = $pagesize*($page-1);
  $sql = "select * from books limit $offset,$pagesize";
  mysql_query("set names utf8");
        $res=mysql_query($sql);
   $rows=array();
  while($row=mysql_fetch_assoc($res)){
    $rows[]=$row;
   }
  $pageInfo = $rows;
  //echo json_encode($pageInfo);
  //var_dump($pageInfo);
  //将获得数据链接,后返回
  $first=1;
  $prev=$page-1;
  $next=$page+1;
  $last=$pages;
  //命令视图层显示数据
      $first_a = "<a onclick='init(".$first.")' href='#'><img src='3.jpg' width='50px;' height='20px;'/></a>";
  if($page>1){
   $prev_a = "<a onclick='init(".$prev.")' href='#'><img src='1.jpg' width='50px;' height='20px;'/></a>";
  }
  if($page<$pages){
   $next_a = "<a onclick='init(".$next.")' href='#'><img src='2.jpg' width='50px;' height='20px;'/></a>";
  }
  $last_a = "<a onclick='init(".$last.")' href='#'><img src='4.jpg' width='50px;' height='20px;'/></a>";
  @$str = $strtext.$first_a.$prev_a.$next_a.$last_a;
  //var_dump($str);
  $info = array('str'=>$str,'info'=>$pageInfo);
  echo json_encode($info);
标签:两个网页,分页功能
0
投稿

猜你喜欢

  • fso对象CreateTextFile方法调用时“无效的过程调用或参数”错误

    2009-05-26 15:39:00
  • python实现逻辑回归的示例

    2022-04-05 05:16:59
  • Dreamweaver 网页编辑常用表现的实现方法

    2010-10-20 20:05:00
  • 利用rpm打包上线部署golang代码的方法教程

    2024-04-26 17:18:26
  • golang简单位运算示例

    2024-02-08 18:53:48
  • DHTML 打造 Picture Spelling

    2013-08-22 17:01:53
  • 轻松搞定IE的CSS制作网页技巧3则

    2009-08-14 20:32:00
  • SQL Server 2008登录错误:无法连接到(local)解决方法

    2024-01-21 00:39:06
  • Python 实现定积分与二重定积分的操作

    2023-08-05 18:22:27
  • JS脚本实现网页自动秒杀点击

    2024-04-16 09:36:09
  • Golang切片Slice功能操作详情

    2024-04-29 13:06:54
  • 解决python Jupyter不能导入外部包问题

    2021-02-02 06:19:03
  • MSSQL中递归SQL查询语句实例说明-

    2024-01-28 19:54:01
  • ASP实现长文章自动分页的函数代码

    2008-10-10 17:09:00
  • [项目布局配置]Nosql与PythonWeb-Flask框架组合

    2022-07-08 19:55:40
  • Python实现批量修改xml文件的脚本

    2022-01-14 06:14:03
  • 关于Javascript闭包与应用的详解

    2024-04-23 09:09:28
  • python实现给微信公众号发送消息的方法

    2021-08-25 23:44:57
  • vite.config.js配置入门详解

    2024-04-30 10:38:25
  • Python selenium环境搭建实现过程解析

    2023-10-06 06:18:48
  • asp之家 网络编程 m.aspxhome.com