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常用代码
Nov 23 PHP
PHP大小写问题:函数名和类名不区分,变量名区分
Jun 17 PHP
PHP实现服务器状态监控的方法
Dec 09 PHP
9个实用的PHP代码片段分享
Jan 22 PHP
php使用ftp远程上传文件类(完美解决主从文件同步问题的方法)
Sep 23 PHP
验证坐标在某坐标区域内php代码
Oct 08 PHP
Zend Framework前端控制器用法示例
Dec 11 PHP
Zend Framework入门应用实例详解
Dec 11 PHP
php传值方式和ajax的验证功能
Mar 27 PHP
windows环境下使用Composer安装ThinkPHP5
May 18 PHP
php实现推荐功能的简单实例
Sep 29 PHP
PHP策略模式写法
Apr 01 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
配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境
2009/04/25 PHP
队列在编程中的实际应用(php)
2010/09/04 PHP
php实现用已经过去多长时间的方式显示时间
2015/06/05 PHP
PHP操作MySQL的mysql_fetch_* 函数的常见用法教程
2015/12/25 PHP
JavaScript中的面向对象介绍
2012/06/30 Javascript
JS数组去重与取重的示例代码
2014/01/24 Javascript
javascript中的事件代理初探
2014/03/08 Javascript
使用jquery修改表单的提交地址基本思路
2014/06/04 Javascript
js判断某个方法是否存在实例代码
2015/01/10 Javascript
JavaScript结合HTML DOM实现联动菜单
2017/04/05 Javascript
微信小程序滚动Tab实现左右可滑动切换
2017/08/17 Javascript
vue实现在v-html的html字符串中绑定事件
2019/10/28 Javascript
vue如何实现动态加载脚本
2020/02/05 Javascript
js对象简介与基本用法示例
2020/03/13 Javascript
js实现有趣的倒计时效果
2021/01/19 Javascript
python批量下载图片的三种方法
2013/04/22 Python
python二分法实现实例
2013/11/21 Python
Python设计足球联赛赛程表程序的思路与简单实现示例
2016/06/28 Python
Python用imghdr模块识别图片格式实例解析
2018/01/11 Python
python实现逆序输出一个数字的示例讲解
2018/06/25 Python
python实现多张图片拼接成大图
2019/01/15 Python
PyCharm的设置方法和第一个Python程序的建立
2019/01/16 Python
Django如何使用第三方服务发送电子邮件
2019/08/14 Python
Python字典底层实现原理详解
2019/12/18 Python
Python使用Paramiko控制liunx第三方库
2020/05/20 Python
python中常见错误及解决方法
2020/06/21 Python
python中如何打包用户自定义模块
2020/09/23 Python
基于python实现监听Rabbitmq系统日志代码示例
2020/11/28 Python
英国手工布艺沙发在线购买:Sofas & Stuff
2018/03/02 全球购物
意大利奢侈品购物网站:Deliberti
2019/10/08 全球购物
期末学生评语大全
2014/04/24 职场文书
党风廉政建设个人总结
2015/03/06 职场文书
学校通报表扬范文
2015/05/04 职场文书
2019如何书写演讲稿?
2019/07/01 职场文书
导游词之山西祁县乔家大院
2019/10/14 职场文书
golang 实用库gotable的具体使用
2021/07/01 Golang