search
Homephp教程php手册我的一个php_mysql分页类

mysql|分页

class RSPage {
  var $Debug    =  0;
  var $sqoe     =  1; // sqoe= show query on error
  var $Link_ID    = 0;
  var $Record    = array();
  var $Row;
  var $numRow;
  var $Parse;
  var $Error     = "";
  var $m_timeFmt = "YYYY-MM-DD HH24:MI:SS";

    var $m_num=-1;
    var $m_sql;
    var $m_pageSize=10;
    var $m_cmt;
    var    $m_curPage=1;
    function open($DB_Link)
    {
        $this->Link_ID = $DB_Link;
    }

    function query($Query_String,$page=1,$cnt=-1,$size=10) {
      if($Query_String=="")
      {
          echo "执行语句不能为空!";
          return false;
      }
        if(!$page)
            $page = 1;
        if(!$cnt)
            $cnt = -1;
        $this->m_pageSize = $size;

      if($this->Parse){
          mysql_free_result($this->Parse);
      }

        if($cnt!=-1){
            $this->m_num = $cnt;
        }
        else{
            $result = mysql_query($Query_String, $this->Link_ID);
            if(!$result){
                $this->Error=mysql_error($this->Link_ID);
            }
            else{
                $this->m_num = mysql_num_rows($result);
            }
            /*
            $cntsql = "select count(*) max_num from ($Query_String)";
            $maxnum = 0;
            $result = mysql_query($sqlstr,$this->Link_ID);
            $ret = mysql_fetch_array($result);
            mysql_free_result($result);
            if(!$ret) {
                $this->Error=mysql_error($this->Link_ID);
            }
            else {
                $this->m_num = $ret["max_num"];
            }
            */
            
        }
        $pageCount = $this->getPageCount();
        $pageSize = $this->getPageSize();
        if($page>$pageCount)$page=$pageCount;
        if($page        $this->m_curPage=$page;
        $posBegin = ($page-1)*$pageSize;
        $posEnd = $posBegin + $pageSize;
        $Query_String = "$Query_String LIMIT $posBegin,$posEnd";
        $this->Parse=mysql_query($Query_String,$this->Link_ID);

      if(!$this->Parse) {
           $this->Error=mysql_error($this->Link_ID);
      }
      $this->Row=0;

      if($this->Debug) {
          printf("Debug: query = %s
\n", $Query_String);
      }

      if ($this->Error && $this->sqoe)
      echo "
".$this->Error["message"]."
Query :\"$Query_String\"
";
      $numRow=mysql_num_rows($this->Parse);
      return $this->Parse;
  }
  function setTimeFormat($timestr){
    $this->m_timeFmt = $timestr;
  }
  function next_record() {
      if(!($row = mysql_fetch_array($this->Parse))) {
          mysql_free_result($this->Parse);
          $this->Parse = false;
          $stat=0;
      }
      else {
          while(list($key,$val)=each($row)) {
              $colreturn=strtolower($key);
              $this->Record[ "$colreturn" ] = $val;
              if($this->Debug) echo"[$key]:".$val."
\n";
          }
          $stat=1;
      }

    return $stat;
  }
  
  function record_exist() {
    if(0 == mysql_num_rows($this->Parse)) {
                return 0;
          } else {
                return 1;
      }

  return $stat;
  }

  function seek($pos) {
      $this->Row=$pos;
  }

  function affected_rows() {
    return mysql_affected_rows($this->Parse);
  }

  function num_rows() {
          return mysql_num_rows($this->Parse);
  }

  function f($Name) {
    return $this->Record[$Name];
  }

  function p($Name) {
    print $this->Record[$Name];
  }

  function close() {
      if($this->Debug) {
          //printf("Disconnecting...
\n");
      }
      if($this->Parse){
          mysql_free_result($this->Parse);
      }
      //mysql_close($this->Link_ID);
  }

  //---public 得到当前页
    function getCurPage(){return $this->m_curPage;}
    //---public 设定页长
    function setPageSize($page_size){$this->m_pageSize = $page_size;}
    //---public 得到页长
    function getPageSize(){return $this->m_pageSize;}
    //---public 得到纪录总数
    function getCount(){return $this->m_num;}
    //---public 得到页总数
    function getPageCount(){
        $page=floor($this->getCount()/$this->getPageSize());
        if($this->getCount()%$this->getPageSize()>0)$page++;
        return $page;
    }
    // 显示导航条。
    // 参数说明: $url    调用页面ex:http://hello.php?showtype=main
    function show_navibar($recname="条记录",$first="首页",$pre="上页",$next="下页",$last="末页"){
        $showurl = getenv("REQUEST_URI");
        $tmppos = strpos($showurl,"&rscnt=");
        if(!$tmppos)
            $tmppos = strpos($showurl,"?rscnt=");
        if($tmppos)
            $showurl = substr($showurl,0,$tmppos);
        $url = $showurl;
        $page = $this->getCurPage();
        $pagecnt = $this->getPageCount();
        $cnt = $this->getCount();
        if($cnt             return;
        
        if(!strrchr($url,"?"))
            $url = $url."?";
        else                            // url已经带了参数。
            $url = $url."&";
        
        $url = $url."rscnt=".$cnt."&page=";
        
        if($page>1){
            ?>
1>=$first?> >=$pre?>
            
        }
        else{
            ?>
            =$first?> =$pre?>
            
        }
        
        if($page            ?>
>=$next?> >=$last?>
            
        }
        else{
            ?>
            =$next?> =$last?>
            
        }
        ?>
        共有 =$cnt?> =$recname?>,当前=$page?>/=$pagecnt?> 
        第>页
        
        
        

    }
}



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools