程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php入門mysql分頁PageQuery類

php入門mysql分頁PageQuery類

編輯:PHP綜合

。<?PHP
include("dbClass.inc");
class PageQuery extends dbClass {
    var $Offset;             // 記錄偏移量
    var $Total;             // 記錄總數
     
    var $maxLine;             // 記錄每頁顯示記錄數
    var $result;             // 讀出的結果
   
    var $TPages;             // 總頁數
    var $CPages;             // 當前頁數
   
    var $PageQuery;         // 分頁顯示要傳遞的參數
    var $Query;             // query 語句
    var $QueryPart;         // " FROM " 以後的 query 部分
    var $QueryString;         // ? 以後部分 
     
    var $FilePath;
           
    // 每頁顯示行數
    function PageQuery($pageLine=10) {   
        $this->dbClass();
        $this->maxLine = $pageLine;
      }
     
      // 記錄總數
    function getTotal(){
        return $this->Total;
    }
     
      // 顯示總頁數
    function getTotalPages() {
        return $this->TPages;
    }

    //顯示當前所在頁數
    function getCurrenPages() {         
        return $this->CPages;
    }
   
    function myQuery($sql, $flag=1){
            GLOBAL $offset;
            $this->Query = $sql;
       
        // 獲取文件名
        //$this->FilePath = $GLOBALS["REQUEST_URI"];
            $this->FilePath = $GLOBALS["SCRIPT_NAME"];
           
            // 獲取查詢條件
            $this->QueryString = $GLOBALS["QUERY_STRING"];           
            //echo $this->QueryString . "<br>";           
           
            // 截取 " from " 以後的 query 語句
   &nbs

MySQL教程是:PHP入門MySQL分頁PageQuery類。p;        $this->QueryPart = trim(strstr($sql, " from "));
           
            // 計算偏移量
            if (!isset($offset)) $this->Offset = 0;
            else $this->Offset = (int)$offset;                            

      // 計算總的記錄條數
        $SQL = "SELECT Count(*) AS total " . $this->QueryPart;
        $this->result = $this->executeQuery($SQL);
            $this->Total = MySQL_result($this->result,0);
           
            // 設置當前頁數和總頁數
        $this->TPages = (double)Ceil((double)$this->Total/$this->maxLine);
        $this->CPages = (double)Floor((double)$this->Offset/$this->maxLine+1);
       
       
        // 根據條件判斷,取出所需記錄
        if ($this->Total > 0) {
            //flag等於1表示要分頁,否則不分頁
            if($flag==1)
                $SQL = $this->Query . " LIMIT " . $this->Offset . " , " . $this->maxLine;
            else
                $SQL = $this->Query;           
            echo $SQL . "<br>";
            $this->$result = $this->executeQuery($SQL);
        }
        return $this->result;
    }
   
    //**********顯示翻頁提示欄************* 
    // 顯示首頁、下頁、上頁、尾頁
    function PageLegend() {       
        $str = "";
        $i = 0;
        $first = 0;
        $next = 0;
        $prev = 0;
        $last = 0;
   
            $next = $this->Offset + $this->maxLine;
            $prev = $this->Offset - $this->maxLine;
            $last = ($this->TPages - 1) * $this->maxLine;
           
         &nb


MySQL教程是:PHP入門MySQL分頁PageQuery類。sp;  GLOBAL $offset;
            if (!isset($offset)) $this->QueryString .= "&offset=";
            else{
                $this->QueryString = substr($this->QueryString,0,strrpos($this->QueryString,''&'')) . "&offset=";
            }
           
            if($this->Offset >= $this->maxLine)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $first . ">首頁</A> ";
            else $str .= " 首頁 ";
       
        if($prev >= 0)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $prev . ">前頁</A> ";
        else $str .= " 前頁 ";
       
        if($next < $this->Total)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $next . ">後頁</A> ";
        else $str .= " 後頁 ";
       
        if($this->TPages != 0 && $this->CPages < $this->TPages)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $last . ">尾頁</A>";
        else $str .= " 尾頁 ";

        $str .= " 頁次:" . $this->getCurrenPages() . "/" . $this->getTotalPages() . "頁 ";
        $str .= $this->maxLine . "條/頁 " . "共" . $this->Total . "條";
            return $str;
    }
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved