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 扭轉乾坤 閱讀(312) 評論(0)  編輯  收藏 所屬分類: JAVA使用技巧

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類(31)

          隨筆檔案(30)

          文章分類(32)

          文章檔案(33)

          相冊

          PHP小站-首頁

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吉木乃县| 平凉市| 绥宁县| 望奎县| 额尔古纳市| 新野县| 成安县| 枞阳县| 阿克| 弋阳县| 雷山县| 泊头市| 辽源市| 南和县| 新巴尔虎右旗| 长岛县| 靖安县| 婺源县| 怀宁县| 教育| 张家口市| 阿拉尔市| 永仁县| 巫溪县| 兴国县| 新闻| 利津县| 津南区| 宜阳县| 轮台县| 安仁县| 玉屏| 临澧县| 泽库县| 阿荣旗| 乐清市| 凭祥市| 南川市| 贵定县| 敦煌市| 黔西|