服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

分页显示oracle数据库记录的类(2.0)


  <?php
  
  class toracleviewpage {
  
  var $table;  //表名
  var $maxline; //每页显示行数
  
  var $linkid;  //数据库连接号
  var $id;    //排序参考字段
  
  var $offset;  //记录偏移量
  var $total;  //记录总数
  var $number;  //本页读取的记录数
  var $topnumber;//读新记录时实际取出的记录数
  var $result;  //读出的结果
  var $topresult;//读新记录时的结果
  
  var $thefirstpage;//特殊指定第一页的链接
  var $startrec;    //指定第二页的起始记录号
  
  var $tpages;  //总页数
  var $cpages;  //当前页数
  
  var $tgroup;
  var $pgroup; //每页显示的页号个数
  var $cgroup;
  
  var $condition;  //显示条件 如:where id='$id' order by id desc
  var $pagequery;  //分页显示要传递的参数
  
  //-------------------------------------
  // 以下构造函数、析构函数及初始化函数
  //-------------------------------------
  
  //构造函数
  //参数:表名、最大行数、分页参考的字段、每页显示的页号数
  
  function toracleviewpage($tb,$ml,$id){
  global $offset;
  
  $this->table=$tb;
  $this->maxline=$ml;
  $this->id=$id;
  
  $this->startrec=0;
  if(isset($offset)) $this->offset=$offset;
  else $this->offset=0;
  
  $this->condition="";
  $this->thefirstpage=null;
  $this->pagequry=null;
  }
  
  //初始化
  //参数:用户名、密码、数据库
  function initdb($user,$password,$db){
  if (php_os == "winnt") $dllid=dl("php3_oci80.dll");
  $this->linkid = ocilogon($user,$password,$db);
  }
  
  //断开
  function destroy(){
  ocilogoff($this->linkid);
  }
  
  //-------------------------
  // set 函数
  //-------------------------
  
  //设置显示条件
  //如:where id='$id' order by id desc
  //要求是字串,符合sql语法(本字串将加在sql语句后)
  
  function setcondition($s){
  $this->condition=$s;
  }
  
  //设置每组的显示个数
  function setnumgroup($pg){
  $this->pgroup=$pg;
  }
  //设置首页,如无则为null
  function setfirstpage($fn){
  $this->thefirstpage=$fn;
  }
  //设置起始记录,如无则取默认0
  function setstartrecord($org){
  $this->startrec=$org;
  }
  
  //设置传递参数
  // key参数名 value参数值
  // 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数。
  
  function setpagequery($key,$value){
  $tmp[key]=$key; $tmp[value]=$value;
  $this->pagequery[]=$tmp;
  }
  
  //--------------------------------
  // get 函数
  //--------------------------------
  
  //取记录总数
  function gettotalrec(){
  
  $sql="select count(*) as total from ".$this->table." ".$this->condition;
  
  $stmt = ociparse($this->linkid,$sql);
  $bool = ociexecute($stmt);
  if (!$bool)  {
  echo "连接失败!";
  ocilogoff($this->linkid);
  exit;
  }
  else   {
  ocifetch($stmt);
  $this->total=ociresult($stmt,1);
  }
  ocifreestatement($stmt);
  }
  
  //取总页数、当前页
  function getpage(){
  $this->tpages=ceil($this->total/$this->maxline);
  $this->cpages=ceil($this->offset/$this->maxline)+1;
  }
  
  //取总组数、当前组
  function getgroup()  {
  $this->tgroup=ceil($this->tpages/$this->pgroup);
  $this->cgroup=ceil($this->cpages/$this->pgroup);
  }
  
  
  //--------------------------------
  // 工作函数
  //--------------------------------
  
  //读取记录
  // 主要工作函数,根据所给的条件从表中读取相应的记录
  // 返回值是一个二维数组,result[记录号][字段名]
  
  function readlist() {
  
  $sql="select * from ".$this->table." ".$this->condition." order by ".$this->id." desc";
  
  $stmt = ociparse($this->linkid,$sql);
  $bool = ociexecute($stmt);
  if (!$bool)  {
  echo "连接失败!";
  ocilogoff($this->linkid);
  exit;
  }
  else   {
  $ncols = ocinumcols($stmt);
  for ( $i = 1; $i <= $ncols; $i++ )
  $column_name[$i] = ocicolumnname($stmt,$i);
  $k=0;
  
  for($j=0;$j<$this->startrec+$this->offset;$j++) ocifetch($stmt);
  for($j=0;$j<$this->maxline;$j++){
  if(ocifetch($stmt)){
  $k++;
  for($i=1;$i<=$ncols;$i++)
  $temp[$column_name[$i]]=ociresult($stmt,$i);
  $this->result[]=$temp;
  }
  else break;
  }
  $this->number=$k;
  
  }
  ocifreestatement($stmt);
  return $this->result;
  }
  
  //读最新的记录
  //topnum指定要读出的记录数
  
  function readtoplist($topnum){
  
  $sql="select * from ".$this->table." ".$this->condition." order by ".$this->id." desc";
  
  $stmt = ociparse($this->linkid,$sql);
  $bool = ociexecute($stmt);
  if (!$bool)  {
  echo "连接失败!";
  ocilogoff($this->linkid);
  exit;
  }
  else  {
  $ncols = ocinumcols($stmt);
  for ( $i = 1; $i <= $ncols; $i++ )
  $column_name[$i] = ocicolumnname($stmt,$i);
  $k=0;
  
  for($j=0;$j<$topnum;$j++){
  if(ocifetch($stmt)){
  $k++;
  for($i=1;$i<=$ncols;$i++)
  $temp[$column_name[$i]]=ociresult($stmt,$i);
  $this->topresult[]=$temp;
  }
  else break;
  }
  $this->topnumber=$k;
  
  }
  ocifreestatement($stmt);
  return $this->topresult;
  
  }
  
  //---------------------------
  // 分页相关
  //---------------------------
  
  //显示当前页及总页数
  //本函数在getpage()后调用。
  function thepage() {
  echo "第".$this->cpages."页/共".$this->tpages."页";
  }
  
  //显示翻页按钮
  //此函数要在getpage()函数之后调用
  //显示下页、上页,并加上要传递的参数
  
  function page() {
  $k=count($this->pagequery);
  $strquery="";  //生成一个要传递参数字串
  for($i=0;$i<$k;$i++){
  $strquery.="&".$this->pagequery[$i][key]."=".$this->pagequery[$i][value];
  }
  
  return $strquery;
  }
  
  function prepage($strquery){
  $prev=$this->offset-$this->maxline;
  if($prev>=0)
  echo "<a href=$php_self?offset=".$prev.$strquery." class=newslink>上一页</a>";
  else if($this->thefirstpage!=null)
  echo "<a href=".$this->thefirstpage." class=newslink>上一页</a>";
  else echo "上一页";
  }
  
  function nexpage($strquery){
  $next=$this->offset+$this->maxline;
  $k=$this->total-$this->startrec;
  if($next<$k)
  echo "<a href=$php_self?offset=".$next.$strquery." class=newslink>下一页</a>";
  else
  echo "下一页";
  }
  
  //------------------------------------
  // 记录分组
  //----------------------------------
  //显示分组
  function numpage()  {
  $first=($this->cgroup-1)*($this->pgroup)+1;
  $last=($first+$this->pgroup > $this->tpages)? ($this->tpages+1):($first+$this->pgroup);
  $pr=($this->cgroup-2>=0)?( ($this->cgroup-2)*($this->pgroup)+1 ):(-1);
  $prev=($pr!=-1)?( ($pr-1)*$this->maxline):(0);
  $ne=($this->cgroup*$this->pgroup+1<=$this->tpages)?($this->cgroup*$this->pgroup+1):(-1);
  $next=($ne!=-1)?( ($ne-1)*$this->maxline):(0);
  
  $k=count($this->pagequery);
  $strquery="";  //生成一个要传递参数字串
  for($i=0;$i<$k;$i++){
  $strquery.="&".$this->pagequery[$i][key]."=".$this->pagequery[$i][value];
  }
  
  if($first!=1)
  echo "<a href=$php_self?offset=".$prev.$strquery." > <<

扫描关注微信公众号