php ajax分页代码
提供一款完美的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; //开始初始化xmlhttprequest对象 if (window.xmlhttprequest){ //mozilla浏览器//开源代码phpfensi.com 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= "<font color='green' font-size='12'>正在读取数据...</font>" ; send_request(url); reobj=obj; } </script> <style> /* css document */ #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; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。 $num =10; //每页显示10条数据 $db =mysql_connect( "localhost" , "root" , "" ); //创建数据库教程连接 mysql_select_db( "test" ); //选择要操作的数据库 /* 首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是 总数据库除以每页显示的条数,有余进一。 也就是说10/3=3.3333=4 有余数就要进一。 */ $result =mysql_query( "select * from users" ); $total =mysql_num_rows( $result ); //查询所有的数据 $url = 'test1.php' ; //获取本页url //页码计算 $pagenum = ceil ( $total / $num ); //获得总页数,也是最后一页 $page =min( $pagenum , $page ); //获得首页 $prepg = $page -1; //上一页 $nextpg =( $page == $pagenum ? 0 : $page +1); //下一页 $offset =( $page -1)* $num ; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。 $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>查看更多关于php ajax分页代码 - php分页的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did27919