一江春水向東流

          做一個有思想的人,期待與每一位熱愛思考的人交流,您的關注是對我最大的支持。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            44 隨筆 :: 139 文章 :: 81 評論 :: 0 Trackbacks

          ?

          <? php
          /*
          ?*?分頁顯示類
          ?*?PageItem.php v?1.0.0
          ?*?編程:Boban<boban@21php.com>
          ?*?討論:http://www.21php.com/forums/
          ?*?更新:2004-02-02
          ?*?說明:
          ?*?1.?配合MYSQL數據庫使用
          ?*?2.?類沒有提供連接數據庫的功能,需在外部建立數據庫連接。
          ?*?
          */
          /*
          ?*?使用方法:
          ?*?$sql?=?"select?*?from?news?limit?0,10";
          ?*?$hdc?=?new?PageItem($sql);
          ?*?echo?$hdc->myPageItem();
          ?*?$arrRecords?=?$hdc->ReadList();
          ?*?
          */
          if ?( ! defined ( " __PAGEITEM__ " ))?{
          ????
          define ( " __PAGEITEM__ " , ? 1 );
          }?
          else ?{
          ????
          exit ( 3 );
          }?

          class ?PageItem?{
          ????
          var ? $iDefaultRecords ? = ? 10 ;? // ?默認每頁顯示記錄數,如果沒有設置,就使用默認


          ????
          var ? $iMaxRecord ;? // 每頁記錄數
          ???? var ? $iTotal ;? // 記錄總數
          ???? var ? $sqlRecord ;? // ?獲取記錄的SQL查詢
          ???? var ? $iPages ;? // 總頁數
          ???? var ? $CPages ;? // 當前頁數
          ???? /*
          ??*?構造函數?--?初始化變量
          ??*?參數:SQL查詢語句,將忽略LIMIT語句
          ??*?
          */

          ????
          function ?PageItem( $sql ? = ? "" )
          ????{?
          ????????
          // ?register_shutdown_function($this->_PageItem());
          ???????? $this -> SetMaxRecord( $this -> iDefaultRecords);
          ????????
          /*
          ???????*?解析SQL語句
          ???????*?
          */
          ????????
          if ?( $sql ? <> ? "" )?{
          ????????????
          list ( $sql )? = ? spliti ( " LIMIT " , ? $sql );? // ?去除LIMIT語句
          ???????????? $this -> sqlRecord? = ? trim ( $sql );
          ????????????
          list ( , ? $sql )? = ? spliti ( " FROM " , ? $sql );
          ????????????
          $sql ? = ? trim ( $sql );
          ???
          if ( preg_match ?( " /\bGROUP\b?\bBY\b/i " , ? $sql ))
          ???{
          ????
          // echo?"HAVE?GROUP?BY";
          ???? if ( preg_match ?( " /\bHAVING\b/i " , ? $sql ))? list ( , $field )? = ? spliti

          (
          " HAVING " , $sql );
          ????
          list ( $field )? = ? spliti ( ' ? ' , trim ( $field ));
          ????
          // echo?$field;
          ???? $this -> iTotal? = ? $this -> CountRecord( " SELECT?$field,COUNT(DISTINCT?$field)?

          AS?cnt?FROM?
          " ? . ? $sql , 2 );
          ???}
          ???
          else ? $this -> iTotal? = ? $this -> CountRecord( " SELECT?COUNT(*)?AS?cnt?FROM? " ? . ?

          $sql , 1 );
          ????????}?
          ????????
          $this -> iPages? = ? ceil ( $this -> iTotal? / ? $this -> iMaxRecord);
          ????????
          $this -> CPages? = ? $_REQUEST [ ' page ' ];
          ????????
          if ?( $this -> CPages? <= ? 0 )? $this -> CPages? = ? 1 ;
          ????????
          if ?( $this -> CPages? > ? $this -> iPages)? $this -> CPages? = ? $this -> iPages;
          ????????
          // echo?"SELECT?COUNT(*)?AS?cnt?FROM?"?.?$sql;
          ??//echo?$this->iTotal;

          ????}?
          ????
          /*
          ?????*?析構函數?--?暫時不可用
          ?????*?
          */
          ????
          function ?_PageItem()
          ????{?
          ????????
          // ?$this->linkid?=?NULL;
          ????}?

          ????
          function ?SetMaxRecord( $cnt )
          ????{
          ????????
          $this -> iMaxRecord? = ? $cnt ;
          ????}?

          ????
          /*
          ?????*?統計匹配的記錄總數
          ?????*?
          */
          ????
          function ?CountRecord( $sql , $type )
          ????{
          ??
          // echo?$sql;
          ?? if ( $type ? == ? 1 )
          ??{
          ???
          if ?(( $records ? = ? mysql_query ( $sql ))? && ?( $record ? = ? mysql_fetch_assoc

          (
          $records )))?{
          ????
          return ? $record [ ' cnt ' ];
          ???}?
          else ? return ? 0 ;
          ??}
          ??
          elseif ( $type ? == ? 2 )
          ??{
          ???
          if ( $records ? = ? mysql_query ( $sql ))
          ????
          return ? mysql_affected_rows ();
          ??}
          ????}
          ?
          /* ?
          ??*?讀取記錄
          ??*?
          */
          ?
          function ?ReadList()
          ?{
          ??
          $ret ? = ? array ();
          ??
          $this -> sqlRecord .= " ?LIMIT? " . ( $this -> CPages - 1 ) * $this -> iMaxRecord . " , " . $this -

          > iMaxRecord;
          ??
          $records ? = ? mysql_query ( $this -> sqlRecord);
          ??
          if ( ! $records )? return ;
          ??
          while ( $record ? = ? mysql_fetch_array ( $records ))
          ??{
          ???
          $ret []? = ? $record ;
          ??}
          ??
          return ? $ret ;
          ?}

          ????
          function ?LinktoPage( $page , ? $msg )
          ????{
          ????????
          $link ? = ? $this -> PageUrl( $page );
          ????????
          return ? " <A?href=\ " $link \ " >$msg</A>\n " ;
          ????}?
          ????
          function ?PageUrl( $page )
          ????{
          ????????
          $phpself ? = ? " http:// " ? . ? $_SERVER [ ' SERVER_NAME ' ]? . ? $_SERVER [ ' PHP_SELF ' ];
          ????????
          $querystring ? = ? $_SERVER [ ' QUERY_STRING ' ];
          ????????
          $querystring ? = ? preg_replace ( " /page=[0-9]*&?/i " , ? "" , ? $querystring );
          ????????
          $link ? = ? $phpself ? . ? " ?page=$page& " ? . ? $querystring ;
          ????????
          return ? $link ;
          ????}?

          ????
          /* ?
          ??*?顯示當前頁及總頁數???
          ??*?
          */
          ????
          function ?PageNav()
          ????{
          ????????
          echo ? " " ? . ? $this -> CPages? . ? " 頁/共 " ? . ? $this -> iPages? . ? " " ;
          ????}?

          ????
          /* ?
          ???*?顯示翻頁按鈕,包括首頁、下頁、上頁、未頁
          ???*?
          */

          ????
          function ?PageButton()
          ????{
          ????????
          if ?( $this -> CPages? > ? 1 )?{
          ????????????
          echo ? $this -> LinktoPage( 1 , ? " 首頁 " );
          ????????????
          echo ? " ?|? " ;
          ????????????
          echo ? $this -> LinktoPage( $this -> CPages - 1 , ? " 上一頁 " );
          ????????}?
          else ?{
          ????????????
          echo ? " 首頁?|?上一頁 " ;
          ????????}?

          ????????
          if ?( $this -> CPages? < ? $this -> iPages)?{
          ????????????
          echo ? " ?|? " ;
          ????????????
          echo ? $this -> LinktoPage( $this -> CPages? + ? 1 , ? " 下一頁 " );
          ????????????
          echo ? " ?|? " ;
          ????????????
          echo ? $this -> LinktoPage( $this -> iPages , ? " 首頁 " );
          ????????}?
          else ?{
          ????????????
          echo ? " ?|?下一頁?|?尾頁 " ;
          ????????}?
          ????}
          ????
          /* ?
          ???*?顯示跳轉頁選擇框
          ???*?
          */

          ????
          function ?SelectItem()
          ????{
          ????????
          echo ? " 跳到第<SELECT?name='topage'?size='1'?

          onchange='window.location=this.value'>\n
          " ;
          ????????
          for ( $i ? = ? 1 ; $i ? <= ? $this -> iPages; $i ++ )?{
          ????????????
          if ?( $this -> CPages? == ? $i )
          ????????????????
          $extra ? = ? " selected " ;
          ????????????
          else
          ????????????????
          $extra ? = ? "" ;
          ????????????
          echo ? " <OPTION?VALUE=' " ? . ? $this -> PageUrl( $i )? . ? " '?

          $extra>$i</OPTION>
          " ;
          ????????}?
          ????????
          echo ? " </SELECT>\n " ;
          ????}

          ????
          /*
          ?????*?一次性顯示所有按鈕組件
          ?????*?
          */
          ????
          function ?myPageItem()
          ????{
          ????????
          $this -> PageButton();
          ????????
          $this -> SelectItem();
          ????????
          $this -> PageNav();
          ????}?
          }?
          // ?類結束

          ?> ???

          <?php
          /*
          ?* 分頁顯示類
          ?* PageItem.php v 1.0.0
          ?* 編程:Boban<boban@21php.com>
          ?* 討論:http://www.21php.com/forums/
          ?* 更新:2004-02-02
          ?* 說明:
          ?* 1. 配合MYSQL數據庫使用
          ?* 2. 類沒有提供連接數據庫的功能,需在外部建立數據庫連接。
          ?* */
          /*
          ?* 使用方法:
          ?* $sql = "select * from news limit 0,10";
          ?* $hdc = new PageItem($sql);
          ?* echo $hdc->myPageItem();
          ?* $arrRecords = $hdc->ReadList();
          ?* */
          if (!defined("__PAGEITEM__")) {
          ??? define("__PAGEITEM__", 1);
          } else {
          ??? exit(3);
          }

          class PageItem {
          ??? var $iDefaultRecords = 10; // 默認每頁顯示記錄數,如果沒有設置,就使用默認


          ??? var $iMaxRecord; //每頁記錄數
          ??? var $iTotal; //記錄總數
          ??? var $sqlRecord; // 獲取記錄的SQL查詢
          ??? var $iPages; //總頁數
          ??? var $CPages; //當前頁數
          ??? /*
          ? * 構造函數 -- 初始化變量
          ? * 參數:SQL查詢語句,將忽略LIMIT語句
          ? * */

          ??? function PageItem($sql = "")
          ??? {
          ??????? // register_shutdown_function($this->_PageItem());
          ??????? $this->SetMaxRecord($this->iDefaultRecords);
          ??????? /*
          ?????? * 解析SQL語句
          ?????? * */
          ??????? if ($sql <> "") {
          ??????????? list($sql) = spliti("LIMIT", $sql); // 去除LIMIT語句
          ??????????? $this->sqlRecord = trim($sql);
          ??????????? list(, $sql) = spliti("FROM", $sql);
          ??????????? $sql = trim($sql);
          ?? if(preg_match ("/\bGROUP\b \bBY\b/i", $sql))
          ?? {
          ??? //echo "HAVE GROUP BY";
          ??? if(preg_match ("/\bHAVING\b/i", $sql)) list(,$field) = spliti

          ("HAVING",$sql);
          ??? list($field) = spliti(' ',trim($field));
          ??? //echo $field;
          ??? $this->iTotal = $this->CountRecord("SELECT $field,COUNT(DISTINCT $field)

          AS cnt FROM " . $sql,2);
          ?? }
          ?? else $this->iTotal = $this->CountRecord("SELECT COUNT(*) AS cnt FROM " .

          $sql,1);
          ??????? }
          ??????? $this->iPages = ceil($this->iTotal / $this->iMaxRecord);
          ??????? $this->CPages = $_REQUEST['page'];
          ??????? if ($this->CPages <= 0) $this->CPages = 1;
          ??????? if ($this->CPages > $this->iPages) $this->CPages = $this->iPages;
          ??????? //echo "SELECT COUNT(*) AS cnt FROM " . $sql;
          ? //echo $this->iTotal;
          ??? }
          ??? /*
          ???? * 析構函數 -- 暫時不可用
          ???? * */
          ??? function _PageItem()
          ??? {
          ??????? // $this->linkid = NULL;
          ??? }

          ??? function SetMaxRecord($cnt)
          ??? {
          ??????? $this->iMaxRecord = $cnt;
          ??? }

          ??? /*
          ???? * 統計匹配的記錄總數
          ???? * */
          ??? function CountRecord($sql,$type)
          ??? {
          ? //echo $sql;
          ? if($type == 1)
          ? {
          ?? if (($records = mysql_query($sql)) && ($record = mysql_fetch_assoc

          ($records))) {
          ??? return $record['cnt'];
          ?? } else return 0;
          ? }
          ? elseif($type == 2)
          ? {
          ?? if($records = mysql_query($sql))
          ??? return mysql_affected_rows();
          ? }
          ??? }
          ?/*
          ? * 讀取記錄
          ? * */
          ?function ReadList()
          ?{
          ? $ret = array();
          ? $this->sqlRecord.=" LIMIT ".($this->CPages-1)*$this->iMaxRecord.",".$this-

          >iMaxRecord;
          ? $records = mysql_query($this->sqlRecord);
          ? if(!$records) return;
          ? while($record = mysql_fetch_array($records))
          ? {
          ?? $ret[] = $record;
          ? }
          ? return $ret;
          ?}

          ??? function LinktoPage($page, $msg)
          ??? {
          ??????? $link = $this->PageUrl($page);
          ??????? return "<A href=\"$link\">$msg</A>\n";
          ??? }
          ??? function PageUrl($page)
          ??? {
          ??????? $phpself = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
          ??????? $querystring = $_SERVER['QUERY_STRING'];
          ??????? $querystring = preg_replace("/page=[0-9]*&?/i", "", $querystring);
          ??????? $link = $phpself . "?page=$page&" . $querystring;
          ??????? return $link;
          ??? }

          ??? /*
          ? * 顯示當前頁及總頁數??
          ? * */
          ??? function PageNav()
          ??? {
          ??????? echo "第" . $this->CPages . "頁/共" . $this->iPages . "頁";
          ??? }

          ??? /*
          ?? * 顯示翻頁按鈕,包括首頁、下頁、上頁、未頁
          ?? * */

          ??? function PageButton()
          ??? {
          ??????? if ($this->CPages > 1) {
          ??????????? echo $this->LinktoPage(1, "首頁");
          ??????????? echo " | ";
          ??????????? echo $this->LinktoPage($this->CPages-1, "上一頁");
          ??????? } else {
          ??????????? echo "首頁 | 上一頁";
          ??????? }

          ??????? if ($this->CPages < $this->iPages) {
          ??????????? echo " | ";
          ??????????? echo $this->LinktoPage($this->CPages + 1, "下一頁");
          ??????????? echo " | ";
          ??????????? echo $this->LinktoPage($this->iPages, "首頁");
          ??????? } else {
          ??????????? echo " | 下一頁 | 尾頁";
          ??????? }
          ??? }
          ??? /*
          ?? * 顯示跳轉頁選擇框
          ?? * */

          ??? function SelectItem()
          ??? {
          ??????? echo "跳到第<SELECT name='topage' size='1'

          onchange='window.location=this.value'>\n";
          ??????? for($i = 1;$i <= $this->iPages;$i++) {
          ??????????? if ($this->CPages == $i)
          ??????????????? $extra = "selected";
          ??????????? else
          ??????????????? $extra = "";
          ??????????? echo "<OPTION VALUE='" . $this->PageUrl($i) . "'

          $extra>$i</OPTION>";
          ??????? }
          ??????? echo "</SELECT>\n";
          ??? }

          ??? /*
          ???? * 一次性顯示所有按鈕組件
          ???? * */
          ??? function myPageItem()
          ??? {
          ??????? $this->PageButton();
          ??????? $this->SelectItem();
          ??????? $this->PageNav();
          ??? }
          } // 類結束

          ?>??

          posted on 2008-05-24 20:00 allic 閱讀(189) 評論(0)  編輯  收藏 所屬分類: PHP+MySQL開發
          主站蜘蛛池模板: 成武县| 凯里市| 呼和浩特市| 福贡县| 蚌埠市| 纳雍县| 拉萨市| 恩施市| 英山县| 罗源县| 津南区| 阿图什市| 元江| 平安县| 会理县| 东海县| 南宫市| 永兴县| 方正县| 浦北县| 五河县| 类乌齐县| 宁陵县| 都兰县| 克山县| 南城县| 石阡县| 扶风县| 海兴县| 偏关县| 铅山县| 涪陵区| 娄底市| 曲水县| 青神县| 广昌县| 长岭县| 和林格尔县| 临邑县| 阿拉善盟| 拜城县|