php和mysql分頁(yè)顯示詳解

          1.數(shù)據(jù)庫(kù)連接的類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.解決分頁(yè)問(wèn)題的PageQuery.inc
          <?php
          include("dbClass.inc");
          class PageQuery extends dbClass {
              private $Offset;             // 記錄偏移量
              private $Total;             // 記錄總數(shù)
               
              private $maxLine;             // 記錄每頁(yè)顯示記錄數(shù)
              private $result;             // 讀出的結(jié)果
             
              private $TPages;             // 總頁(yè)數(shù)
              private $CPages;             // 當(dāng)前頁(yè)數(shù)
             
              private $PageQuery;         // 分頁(yè)顯示要傳遞的參數(shù)
              private $Query;             // query 語(yǔ)句
              private $QueryPart;         // " FROM " 以后的 query 部分
              private $QueryString;         // ? 以后部分 
               
              private $FilePath;
                     
              // 每頁(yè)顯示行數(shù)
              function PageQuery($pageLine=10) {   
                  $this->dbClass();
                  $this->maxLine = $pageLine;
                }
               
                // 記錄總數(shù)
              function getTotal(){
                  return $this->Total;
              }
               
                // 顯示總頁(yè)數(shù)
              function getTotalPages() {
                  return $this->TPages;
              }

              //顯示當(dāng)前所在頁(yè)數(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 語(yǔ)句
                      $this->QueryPart = trim(strstr($sql, " from "));
                     
                      // 計(jì)算偏移量
                      if (!isset($offset)) $this->Offset = 0;
                      else $this->Offset = (int)$offset;
                     
                     
                 
                 
                  // 計(jì)算總的記錄條數(shù)
                  $SQL = "SELECT Count(*) AS total " . $this->QueryPart;
                  $this->result = $this->executeQuery($SQL);
                      $this->Total = mysql_result($this->result,0);
                     
                      // 設(shè)置當(dāng)前頁(yè)數(shù)和總頁(yè)數(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表示要分頁(yè),否則不分頁(yè)
                      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;
              }
             
              //**********顯示翻頁(yè)提示欄************* 
              // 顯示首頁(yè)、下頁(yè)、上頁(yè)、尾頁(yè)
              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 . ">首頁(yè)</A> ";
                      else $str .= " 首頁(yè) ";
                 
                  if($prev >= 0)
                      $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $prev . ">上一頁(yè)</A> ";
                  else $str .= " 上一頁(yè) ";
                 
                  if($next < $this->Total)
                      $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $next . ">下一頁(yè)</A> ";
                  else $str .= " 下一頁(yè) ";
                 
                  if($this->TPages != 0 && $this->CPages < $this->TPages)
                      $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $last . ">尾頁(yè)</A>";
                  else $str .= " 尾頁(yè) ";

                  $str .= " 頁(yè)次:" . $this->getCurrenPages() . "/" . $this->getTotalPages() . "頁(yè) ";
                  $str .= $this->maxLine . "條/頁(yè) " . "共" . $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.顯示頁(yè)面的代碼:
          <!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分頁(yè)顯示</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); // 翻頁(yè)欄
          ?>
          </body>
          </html>

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

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類(31)

          隨筆檔案(30)

          文章分類(32)

          文章檔案(33)

          相冊(cè)

          PHP小站-首頁(yè)

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 泾川县| 怀柔区| 宁乡县| 临桂县| 阳谷县| 大石桥市| 富源县| 武平县| 神木县| 唐山市| 舒兰市| 肥东县| 吉安市| 蓝山县| 高州市| 彭州市| 渝中区| 凭祥市| 神农架林区| 武平县| 兴山县| 凌源市| 工布江达县| 开阳县| 怀宁县| 西贡区| 淅川县| 宝应县| 呈贡县| 昌图县| 西充县| 台北市| 河南省| 昌都县| 灵石县| 介休市| 安阳县| 云南省| 浪卡子县| 湖北省| 宽城|