提供一款完美的php ajax分页程序,如果你正在愁这个就进来看看吧,好了费话不说多喜欢ajax朋友来吧,代码如下:
- <?php
- header("content-type: text/html;charset=gbk");
- ?>
- <html>
- <head>
- <title>ajax分页演示</title>
- <script language="javascript" >
- var http_request=false;
- function send_request(url){
- http_request=false;
-
- if(window.xmlhttprequest){
- http_request=new xmlhttprequest();
- if(http_request.overridemimetype){
- http_request.overridemimetype("text/xml");
- }
- }
- else if(window.activexobject){
- 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;
-
- 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="<font color='green' font-size='12'>正在读取数据...</font>";
- send_request(url);
- reobj=obj;
- }
-
- </script>
- <style>
-
- #result ul li{
- height:20px;
- width:auto;
- display:block;
- color:#999;
- border:1px solid #999;
- float:left;
- list-style:none;
- font-size:12px;
- margin-left:5px;
- line-height:20px;
- vertical-align:middle;
- text-align:center;
- }
- #result ul li a:link{
- width:50px;
- height:20px;
- display:block;
-
- line-height:20px;
- background:#09c;
- border:1px solid #fff;
- color:#fff;
- text-decoration:none;
- }
- #result ul li a:hover{
- width:50px;
- height:20px;
- display:block;
-
- line-height:20px;
- background:#09c;
- border:1px solid #fff;
- color:#f60;
- text-decoration:none;
- }
- </style>
- </head>
- <body>
- <div id="result">
- <?php
- $page=isset($_get['page'])?intval($_get['page']):1;
- $num=10;
-
- $db=mysql_connect("localhost","root","");
- mysql_select_db("test");
-
-
-
-
-
-
-
- $result=mysql_query("select * from users");
- $total=mysql_num_rows($result);
-
- $url='test1.php';
-
-
- $pagenum=ceil($total/$num);
- $page=min($pagenum,$page);
- $prepg=$page-1;
- $nextpg=($page==$pagenum ? 0 : $page+1);
- $offset=($page-1)*$num;
- $pagenav="<ul>";
-
-
- $pagenav.="<li>显示第 <b>".($total?($offset+1):0)."</b>-<b>".min($offset+10,$total)."</b> 条记录</li><li>共 $total 条记录 </li>";
-
-
- if($pagenum<=1) return false;
-
- $pagenav.="<li> <a href=javascript:dopage('result','$url?page=1');>首页</a></li> ";
- if($prepg) $pagenav.="<li> <a href=javascript:dopage('result','$url?page=$prepg');>前页</a></li> "; else $pagenav.=" <li>前页</li> ";
- if($nextpg) $pagenav.="<li><a href=javascript:dopage('result','$url?page=$nextpg');>后页</a> </li>"; else $pagenav.=" <li>后页</li> ";
- $pagenav.="<li> <a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a></li> ";
- $pagenav.="<li>第 $page 页</li><li>共 $pagenum 页</li></ul>";
-
-
- if($page>$pagenum){
- echo "error : can not found the page ".$page;
- exit;
- }
-
- $info=mysql_query("select * from users limit $offset,$num");
- while($it=mysql_fetch_array($info)){
- echo $it['u_name'];
- echo "<br>";
- }
- echo"<br>";
- echo $pagenav;
-
- ?>
- </div>
- </body>
- </html>
|