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


Posted in PHP onApril 16, 2013

分页的首页

<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);
PHP 相关文章推荐
PHP 选项及相关信息函数库
Dec 04 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
Aug 05 PHP
php一行代码获取文件后缀名实例分析
Nov 12 PHP
codeigniter中实现一次性加载多个view的方法
Mar 20 PHP
Yii2中事务的使用实例代码详解
Sep 07 PHP
解决Yii2邮件发送结果返回成功,但接收不到邮件的问题
May 23 PHP
php实现socket推送技术的示例
Dec 20 PHP
thinkphp5 加载静态资源路径与常量的方法
Dec 24 PHP
bindParam和bindValue的区别以及在Yii2中的使用详解
Mar 12 PHP
PHP hebrev()函数用法讲解
Feb 21 PHP
php抽象类和接口知识点整理总结
Aug 02 PHP
Laravel timestamps 设置为unix时间戳的方法
Oct 11 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
Apr 15 #PHP
关于PHP的相似度计算函数:levenshtein的使用介绍
Apr 15 #PHP
关于PHP递归算法和应用方法介绍
Apr 15 #PHP
PHP 读取Postgresql中的数组
Apr 14 #PHP
php简单开启gzip压缩方法(zlib.output_compression)
Apr 13 #PHP
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
Apr 13 #PHP
php检测图片木马多进制编程实践
Apr 11 #PHP
You might like
php仿discuz分页效果代码
2008/10/02 PHP
Yii使用ajax验证显示错误messagebox的解决方法
2014/12/03 PHP
PHP实现活动人选抽奖功能
2017/04/19 PHP
php-fpm中max_children的配置
2019/03/15 PHP
javascript:void(0)的真正含义实例分析
2008/08/20 Javascript
JQuery在光标位置插入内容的实现代码
2010/06/18 Javascript
jquery提升性能最佳实践小结
2010/12/06 Javascript
使用AngularJS实现可伸缩的页面切换的方法
2015/06/19 Javascript
JavaScript forEach()遍历函数使用及介绍
2015/07/08 Javascript
AngularJS 避繁就简的路由
2016/07/01 Javascript
JS实现灯泡开关特效
2020/03/30 Javascript
python中执行shell命令的几个方法小结
2014/09/18 Python
python中星号变量的几种特殊用法
2016/09/07 Python
python常见排序算法基础教程
2017/04/13 Python
python+pyqt实现12306图片验证效果
2017/10/25 Python
Python和Java进行DES加密和解密的实例
2018/01/09 Python
Python函数的参数常见分类与用法实例详解
2019/03/30 Python
python二进制文件的转译详解
2019/07/03 Python
使用python绘制温度变化雷达图
2019/10/18 Python
Python爬虫解析网页的4种方式实例及原理解析
2019/12/30 Python
django之导入并执行自定义的函数模块图解
2020/04/01 Python
CSS3提交意见输入框样式代码
2014/10/30 HTML / CSS
CHARLES & KEITH英国官网:新加坡时尚品牌
2018/07/04 全球购物
公司领导推荐信
2013/11/12 职场文书
资产评估专业学生的自我鉴定
2013/11/14 职场文书
体育专业个人求职信范文
2013/12/27 职场文书
大二学生职业生涯规划书
2014/02/05 职场文书
益达广告词
2014/03/14 职场文书
教职工代表大会主持词
2014/04/01 职场文书
《李时珍夜宿古寺》教学反思
2014/04/09 职场文书
工商企业管理专业自荐信范文
2014/04/12 职场文书
思想作风纪律整顿心得体会
2014/09/04 职场文书
医德医风个人工作总结2014
2014/11/14 职场文书
努力工作保证书
2015/02/28 职场文书
大学开学典礼新闻稿
2015/07/17 职场文书
应届毕业生的自我评价
2019/06/21 职场文书