Ajax的无刷新分页

Ajax的无刷新分页

 

这里以两个文件进行代码显示一个是ajax分页实现的文件,另一个是分页类文件

 

文件1.ajax_page.php

<script type="text/javascript" language="javascript">
 function createXMLHttp()
 {
  var browser=navigator.appName; //get the current browser
  if(browser=="Microsoft Internet Explorer")
  {
   xmlHttp=new ActiveXObject("Microsoft.XMLHttp"); //if is IE
  }
  else xmlHttp=new XMLHttpRequest();
 }

 function changePage(url)
 {
  xmlHttp.onreadystatechange=newPage;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
 }

 function newPage()
 {
  if(xmlHttp.readyState==4)
  {
   if(xmlHttp.status==200)
   {
    document.getElementById("newPage").innerHTML=xmlHttp.responseText;
   }
  }
 }

 function doPage(url)
 {
  var xmlHttp=false;
  createXMLHttp();
  document.getElementById("newPage").innerHTML="正在读取数据...";
  changePage(url);
 }


</script>
<style type="text/css">
#pageWay{
 background-color:#b5d6e6; background-image:url("tab_19.gif");  width:100%; font-size:15px; padding:5px;
}
span{
 color:red; font-weight:700;
}
.one{
  width:37px; height:15px;
}
.two{
 width:43px; height:15px;
}
img{
 border:0px;
}

</style>

<div id="newPage">
<?php
  include "database.class.php";
  include "page.class.php";
  $db=new database();
  $data_sql="SELECT `exptime`,`realdat`,`predat` FROM `labrec`";
   $data_res=$db->query($data_sql) or die($db->error());
   $data_nums=$db->num_rows($data_res); //求出总的数据条数
   if($data_nums)
   {
     $page_size=15;     //默认每页显示的数据条数
     $page=new page($page_size,$data_nums);
   }
   echo "<table width=100% border=0 cellpadding=3 cellspacing=1 bgcolor=#b5d6e6>/n";
            echo "<tr align=center><td>实验时间</td><td>实测值</td><td>给定值</td></tr>/n";
   if($data_nums)
   {
      $page->show_data();           //输出表格数据

   }
   else echo "<tr><td colspan='3' align='center'>暂无实验数据</td></tr>/n";
   echo "</table>/n";

 

   echo "<div id=pageWay>";
   $page->page_way_1(); $page->page_way_2();//显示分页方式
   echo "</div>/n";
?>
 </div>

 

 文件2.page.class.php

   <?php
 class page
 {
  private $pagesize;
  private $lastpage;
  private $totalpages;
  private $nums;
  private $numPage=1;

 

  public function __construct($page_size,$total_nums)
  {
   $this->pagesize=$page_size;
   $this->nums=$total_nums;
   $this->lastpage=ceil($this->nums/$this->pagesize);
   $this->totalpages=ceil($this->nums/$this->pagesize);
   if(!empty($_GET[page]))
   {
    $this->numPage=$_GET[page];
    if(!is_int($this->numPage)) $this->numPage=(int)$this->numPage;
    if($this->numPage<1) $this->numPage=1;
    if($this->numPage>$this->lastpage) $this->numPage=$this->lastpage;
   }
  }

  //exp table 数据的分页结果
  public function show_data()
  {
   global $db;
   $row_num=(($this->numPage)-1) * $this->pagesize;
   $row_num=$row_num.",";
   $sql="SELECT `exptime`,`realdat`,`predat` FROM `labrec` LIMIT $row_num $this->pagesize";
   $res=$db->query($sql);
   while($row=$db->fetch_array($res))
   {
    $exptime=$row['exptime'];
    $realdat=$row['realdat'];
    $predat=$row['predat'];
    echo "<tr align=center bgcolor=#FFFFFF height=20 >/n";
    echo "<td>$exptime</td><td>$realdat</td><td>$predat</td>";
    echo "</tr>/n";
   }
  }

  public function page_way_1()
  {
   $url=$_SERVER['REQUEST_URI'];
   $url=parse_url($url);
   $filename=basename($url['path']);
   $pagetoload=$url['query'];
   if($pagetoload)
   {
    if(strpos($pagetoload,"&page")!==false)
    {
     $pos=strpos($pagetoload,"&page");

     $pagetoload=substr($pagetoload,0,$pos);
     $path=$filename."?".$pagetoload."&page";
    }
    else $path=$filename."?".$pagetoload."&page";
   }
   else
   {
    $path=$filename."?page";
   }
   if($this->nums > $this->pagesize)
   {
    echo "共<span>$this->totalpages</span>页 第<span>$this->numPage</span>页 共<span>$this->nums</span>条/n";
    if($this->numPage==1)
    {
     echo "<img src='first.gif' class='one'/>/n"; //首页
     echo "<img src='back.gif' class='two'/>/n"; //上一页
    }
    if($this->numPage >= 2 && $this->numPage <= $this->lastpage)
    {
     echo "<a href=javascript:doPage('$path=1')><img src=first.gif  class='one'/></a>/n" ; //首页
     echo "<a href=javascript:doPage('$path=".($this->numPage-1)."')><img src=back.gif class='two'/></a>/n" ; //上一页
    }

    if($this->numPage==$this->lastpage)
    {
     echo "<img src='next.gif' class='two'/>";   //下一页
     echo "<img src='last.gif' class='one'/>";//尾页
    }
    if($this->numPage >= 1 && $this->numPage < $this->lastpage)
    {
     echo "<a href=javascript:doPage('$path=".($this->numPage+1)."')><img src='next.gif'  class='two'/></a>";//下一页
     echo "<a href=javascript:doPage('$path=$this->lastpage')><img src='last.gif' class='one'/></a>";   //尾页
    }
   }
  }

  public function page_way_2()
  {
   $url=$_SERVER['REQUEST_URI'];
   $url=parse_url($url);
   $filename=basename($url['path']);
   $pagetoload=$url['query'];
   if($pagetoload)
   {
    if(strpos($pagetoload,"&page")!==false)
    {
     $pos=strpos($pagetoload,"&page");

     $pagetoload=substr($pagetoload,0,$pos);
     $path=$filename."?".$pagetoload."&page";
    }
    else $path=$filename."?".$pagetoload."&page";
   }
   else
   {
    $path=$filename."?page";
   }
   if($this->nums > $this->pagesize)
   {
    echo "到<select name='select1' onChange=/"doPage('$path='+this.value)/">";
    for($i = 1;$i <= $this->totalpages;$i++)
    echo "<option value='" . $i . "'" . (($this->numPage == $i) ? 'selected' : '') . ">" . $i . "</option>";
         echo "</select>&nbsp;页&nbsp;";
   }

  }

 


 }


?>

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值