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

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

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

    php和mysql分頁顯示詳解

    1.數(shù)據(jù)庫連接的類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;             // 記錄總數(shù)
         
        private $maxLine;             // 記錄每頁顯示記錄數(shù)
        private $result;             // 讀出的結(jié)果
       
        private $TPages;             // 總頁數(shù)
        private $CPages;             // 當前頁數(shù)
       
        private $PageQuery;         // 分頁顯示要傳遞的參數(shù)
        private $Query;             // query 語句
        private $QueryPart;         // " FROM " 以后的 query 部分
        private $QueryString;         // ? 以后部分 
         
        private $FilePath;
               
        // 每頁顯示行數(shù)
        function PageQuery($pageLine=10) {   
            $this->dbClass();
            $this->maxLine = $pageLine;
          }
         
          // 記錄總數(shù)
        function getTotal(){
            return $this->Total;
        }
         
          // 顯示總頁數(shù)
        function getTotalPages() {
            return $this->TPages;
        }

        //顯示當前所在頁數(shù)
        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;
               
               
           
           
            // 計算總的記錄條數(shù)
            $SQL = "SELECT Count(*) AS total " . $this->QueryPart;
            $this->result = $this->executeQuery($SQL);
                $this->Total = mysql_result($this->result,0);
               
                // 設(shè)置當前頁數(shù)和總頁數(shù)
            $this->TPages = (double)Ceil((double)$this->Total/$this->maxLine);
            $this->CPages = (double)Floor((double)$this->Offset/$this->maxLine+1);
           
           
            // 根據(jù)條件判斷,取出所需記錄
            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.用于顯示結(jié)果的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"); // 執(zhí)行查詢

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

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

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

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 美女18一级毛片免费看| 亚洲av成人一区二区三区观看在线| 美女黄频免费网站| 国产特级淫片免费看| 亚洲精品9999久久久久无码| 免费无码不卡视频在线观看| 亚洲精品无码mⅴ在线观看| 免费精品人在线二线三线区别| 亚洲av无码专区在线| 成人午夜18免费看| 亚洲av日韩av永久在线观看| 亚洲AⅤ视频一区二区三区| 免费无码专区毛片高潮喷水| 久久久久久亚洲精品不卡| 国产在线一区二区综合免费视频| 久久亚洲免费视频| 日本成年免费网站| 国产亚洲一卡2卡3卡4卡新区| 免费**毛片在线播放直播 | 国产AV旡码专区亚洲AV苍井空| 成人免费无码大片a毛片| 精品亚洲成A人在线观看青青| 亚洲日本中文字幕一区二区三区| 国产VA免费精品高清在线| 亚洲成人动漫在线| 成年女人毛片免费播放视频m| 污网站免费在线观看| 亚洲国产精品无码中文字| 最近最新高清免费中文字幕| 亚洲欧美一区二区三区日产| 亚洲精品乱码久久久久久不卡| 成人黄软件网18免费下载成人黄18免费视频| 久久精品国产亚洲AV久| 亚洲av无码专区在线观看素人| 免费A级毛片av无码| 亚洲国产精品无码久久98| 亚洲精品无码mv在线观看网站| 免费大片黄在线观看yw| 国产精品九九久久免费视频| 亚洲国产超清无码专区| 在线观看亚洲精品国产|