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一些服务器端特性的配置加强php的安全
Oct 09 PHP
PHP获取文件绝对路径的代码(上一级目录)
May 29 PHP
PHP文章按日期(月日)SQL归档语句
Nov 29 PHP
用php制作简单分页(从数据库读取记录)的方法详解
May 04 PHP
php.ini修改php上传文件大小限制的方法详解
Jun 17 PHP
php setcookie函数的参数说明及其用法
Apr 20 PHP
php判断当前用户已在别处登录的方法
Jan 06 PHP
PHP模板引擎Smarty内建函数详解
Apr 11 PHP
让你的PHP7更快之Hugepage用法分析
May 31 PHP
php实现文章置顶功能的方法
Oct 20 PHP
PHP自动载入类文件函数__autoload的使用方法
Mar 25 PHP
PHP扩展类型及安装方式解析
Apr 27 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连接函数implode与分割explode的深入解析
2013/06/26 PHP
PHP Session 变量的使用方法详解与实例代码
2013/09/11 PHP
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
WordPress中注册菜单与调用菜单的方法详解
2015/12/18 PHP
CentOS下搭建PHP环境与WordPress博客程序的全流程总结
2016/05/07 PHP
SCP远程VPS快速搬家和WDCP升级php5.3安装memcached和eaccelerator教程
2017/07/27 PHP
推荐:极酷右键菜单
2006/11/29 Javascript
IE6/7/8/9不支持exec的简写方式
2011/05/25 Javascript
基于jquery的代码显示区域自动拉长效果
2011/12/07 Javascript
Javascript脚本实现静态网页加密实例代码
2013/11/05 Javascript
javascript中数组array及string的方法总结
2014/11/28 Javascript
javascript生成随机数方法汇总
2015/11/12 Javascript
jQuery简介_动力节点Java学院整理
2017/07/04 jQuery
ztree简介_动力节点Java学院整理
2017/07/19 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
bootstrap 点击空白处popover弹出框隐藏实例
2018/01/24 Javascript
Vue自定义组件的四种方式示例详解
2020/02/28 Javascript
基于Cesium绘制抛物弧线
2020/11/18 Javascript
[47:20]DAC2018 4.4 淘汰赛 Optic vs Mineski 第一场
2018/04/05 DOTA
400多行Python代码实现了一个FTP服务器
2012/05/10 Python
在Python的Django框架中创建语言文件
2015/07/27 Python
Python实现求两个csv文件交集的方法
2017/09/06 Python
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
2018/01/11 Python
TensorFlow实现iris数据集线性回归
2018/09/07 Python
将string类型的数据类型转换为spark rdd时报错的解决方法
2019/02/18 Python
OpenCV 模板匹配
2019/07/10 Python
Python循环结构的应用场景详解
2019/07/11 Python
详解如何修改python中字典的键和值
2020/09/29 Python
python爬虫用request库处理cookie的实例讲解
2021/02/20 Python
html5 canvas里绘制椭圆并保持线条粗细均匀的技巧
2013/03/25 HTML / CSS
美国新蛋IT数码商城:Newegg.com
2016/07/21 全球购物
彪马美国官网:PUMA美国
2017/03/09 全球购物
《草原的早晨》教学反思
2014/04/08 职场文书
模具设计与制造专业求职信
2014/07/19 职场文书
学习作风建设心得体会
2014/10/22 职场文书
2015年食品安全宣传周活动总结
2015/07/09 职场文书