<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    php和mysql分頁顯示詳解

    1.數據庫連接的類dbClass.inc。
    <?php
    /**
    * a class use to connect the MySQL database and do some query
    */
    class dbClass {
    private $hostName = "localhost:3306";
    private $dbName = "ebooklib";
    private $Login = "root";
    private $Password = "";
    private $conn;
    private $result;

    function dbClass(){
    $this->conn = mysql_connect("$this->hostName","$this->Login","$this->Password");
    mysql_select_db("$this->dbName", $this->conn);
    }

    function executeQuery($sql){
    $this->result = mysql_query("$sql",$this->conn);
    return $this->result;
    }

    function closeConn(){
    mysql_close($this->conn);
    }
    }

    ?>

    2.解決分頁問題的PageQuery.inc
    <?php
    include("dbClass.inc");
    class PageQuery extends dbClass {
        private $Offset;             // 記錄偏移量
        private $Total;             // 記錄總數
         
        private $maxLine;             // 記錄每頁顯示記錄數
        private $result;             // 讀出的結果
       
        private $TPages;             // 總頁數
        private $CPages;             // 當前頁數
       
        private $PageQuery;         // 分頁顯示要傳遞的參數
        private $Query;             // query 語句
        private $QueryPart;         // " FROM " 以后的 query 部分
        private $QueryString;         // ? 以后部分 
         
        private $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;
        }
    }
    ?>
    3.用于顯示結果的mysql_result_all.inc
    <?
    function mysql_result_all($result,$format="") {
    echo "<table $format><tr>";
    for($i=0;$i<mysql_num_fields($result);$i++) {
    echo "<th>".mysql_field_name($result,$i)."</th>";
    }
    echo "</tr>";
    while($row = mysql_fetch_array($result) ) {
    for($i=0;$i<mysql_num_fields($result);$i++) {
    echo "<td>".$row[$i]."</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    }
    ?>
    4.顯示頁面的代碼:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>php&mysql分頁顯示</title>
    </head>

    <body>
    <?php
    include("PageQuery.inc");

    $pq = new PageQuery(5); // 獲取Connection
    $res=$pq->myQuery("select * from users"); // 執行查詢

    require("mysql_result_all.inc");
    mysql_result_all($res,"border=1");
    echo $pq->PageLegend(2); // 翻頁欄
    ?>
    </body>
    </html>

    posted on 2005-09-01 17:21 扭轉乾坤 閱讀(307) 評論(0)  編輯  收藏 所屬分類: JAVA使用技巧

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产无遮挡色视频免费视频| 成人特黄a级毛片免费视频| 免费国产成人午夜私人影视| 国产精品亚洲精品| 曰批视频免费30分钟成人| 亚洲国产精品综合一区在线| 亚洲免费中文字幕| 亚洲youjizz| 啦啦啦手机完整免费高清观看| 国产成+人+综合+亚洲专| 四虎永久在线精品免费网址| 亚洲人成未满十八禁网站| 四虎影在线永久免费四虎地址8848aa | 久久激情亚洲精品无码?V| 九九综合VA免费看| 亚洲AV无码成人精品区在线观看 | 亚洲韩国—中文字幕| 99精品免费观看| 亚洲香蕉久久一区二区三区四区| 在线v片免费观看视频| 亚洲av永久无码精品秋霞电影秋 | 亚洲AV无码不卡在线观看下载| 免费在线观看亚洲| 亚洲人成色7777在线观看| 免费无码H肉动漫在线观看麻豆| 久久亚洲成a人片| 青草草色A免费观看在线| 亚洲国产午夜精品理论片在线播放 | 波多野结衣一区二区免费视频| 亚洲第一视频在线观看免费| 亚洲AV无码码潮喷在线观看| 搡女人真爽免费视频大全| 男女交性无遮挡免费视频| 亚洲第一精品在线视频| 免费无码A片一区二三区| 一个人看的hd免费视频| 亚洲视频在线观看网站| 亚洲成av人片不卡无码久久| 99热在线观看免费| 免费福利资源站在线视频| 7777久久亚洲中文字幕蜜桃|