php新手之mysql分頁PageQuery類

發表于:2007-05-25來源:作者:點擊數: 標簽:MySQLphpPageQuery分頁新手
提供完全可以使用的類,使用時請初始化各函數! ? php include(dbClass.inc); class PageQuery extends dbClass { var $Offset;// 記錄偏移量 var $Total;// 記錄總數 var $maxLine;// 記錄每頁顯示記錄數 var $result;// 讀出的結果 var $TPages;// 總頁數
  提供完全可以使用的類,使用時請初始化各函數!

<?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 語句
            $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;
            
            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;
    }
}
?>

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97