搜尋
首頁php教程PHP源码php的ajax分页

php的ajax分页

May 25, 2016 pm 05:12 PM


1. [文件]     sql.sql 

CREATE TABLE `tb_user` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;


INSERT INTO `tb_user` VALUES (1, 'aaa');
INSERT INTO `tb_user` VALUES (2, 'bbb');
INSERT INTO `tb_user` VALUES (3, 'ccc');
INSERT INTO `tb_user` VALUES (4, 'ddd');
INSERT INTO `tb_user` VALUES (5, 'eee');
INSERT INTO `tb_user` VALUES (6, 'fff');
INSERT INTO `tb_user` VALUES (7, 'ggg');
INSERT INTO `tb_user` VALUES (8, 'hhh');
INSERT INTO `tb_user` VALUES (9, 'жпнд');

                                        

2. [文件]     ajaxpage.js 

var http_request=false;
  function send_request(url){//初始化,指定处理函数,发送请求的函数
    http_request=false;
    //开始初始化XMLHttpRequest对象
    if(window.XMLHttpRequest){//Mozilla浏览器
     http_request=new XMLHttpRequest();
     if(http_request.overrideMimeType){//设置MIME类别
       http_request.overrideMimeType("text/xml");
     }
    }
    else if(window.ActiveXObject){//IE浏览器
     try{
      http_request=new ActiveXObject("Msxml2.XMLHttp");
     }catch(e){
      try{
      http_request=new ActiveXobject("Microsoft.XMLHttp");
      }catch(e){}
     }
    }
    if(!http_request){//异常,创建对象实例失败
     window.alert("创建XMLHttp对象失败!");
     return false;
    }
    http_request.onreadystatechange=processrequest;
    //确定发送请求方式,URL,及是否同步执行下段代码
    http_request.open("GET",url,true);
    http_request.send(null);
  }
  //处理返回信息的函数
  function processrequest(){
   if(http_request.readyState==4){//判断对象状态
     if(http_request.status==200){//信息已成功返回,开始处理信息
      document.getElementById(reobj).innerHTML=http_request.responseText;
     }
     else{//页面不正常
      alert("您所请求的页面不正常!");
     }
   }
  }
  function dopage(obj,url){
   document.getElementById(obj).innerHTML="正在读取数据...";
   reobj = obj;
   send_request(url);
   }

                                          

3. [代码][PHP]代码  

<title>PHP+ajax分页演示</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="javascript" src="ajaxpage.js"></script>

<p id="result">
<?php
$terry=mysql_connect("localhost","root","")or die("连接数据库失败:".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("set NAMES &#39;utf8&#39;");
$result=mysql_query("select * from tb_user");

$total=mysql_num_rows($result) or die(mysql_error());

$page=isset($_GET[&#39;page&#39;])?intval($_GET[&#39;page&#39;]):1;
$page_size=3;
$url=&#39;index.php&#39;;

$pagenum=ceil($total/$page_size);
$page=min($pagenum,$page);
$prepage=$page-1;
$nextpage=($page==$pagenum?0:$page+1);
$pageset=($page-1)*$page_size;
$pagenav=&#39;&#39;;
$pagenav.="显示第<font color=&#39;red&#39;>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录 共<b><font color=&#39;yellow&#39;>".$total."</font></b>条记录 现在是第 <b><font color=&#39;blue&#39;>".$page."</font></b> 页 ";
if($page<=1)
$pagenav.="<a style=cursor:not-allowed;>首页</a> ";
else
$pagenav.="<a onclick=javascript:dopage(&#39;result&#39;,&#39;$url?page=1&#39;) style=cursor:pointer;>首页</a> ";
if($prepage)
$pagenav.="<a onclick=javascript:dopage(&#39;result&#39;,&#39;$url?page=$prepage&#39;) style=cursor:pointer;>上一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>上一页</a> ";
if($nextpage)
$pagenav.="<a onclick=javascript:dopage(&#39;result&#39;,&#39;$url?page=$nextpage&#39;) style=cursor:pointer;>下一页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>下一页</a> ";
if($pagenum)
$pagenav.="<a onclick=javascript:dopage(&#39;result&#39;,&#39;$url?page=$pagenum&#39;) style=cursor:pointer;>尾页</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>尾页</a> ";
$pagenav.="共".$pagenum."页";

if($page>$pagenum){
    echo "error:没有此页".$page;
    exit();
}
?>
<table align="center" border="2" width="300">
  <tr bgcolor="#cccccc" align="center">
    <td>用户名</td>
    <td>用户密码</td>
  </tr>
<?php
$info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
  <tr align="center">
    <td><?php echo $array[&#39;id&#39;];?></td>
    <td><?php echo $array[&#39;username&#39;];?></td>
  </tr>
<?php    
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>";
?>
</p>

                   


                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境