- <?php
- /*
- * 分頁顯示類
- * PageItem.php v 1.0.0
- * 編程:Boban<boban@21php.com>
- * 討論:http://www.49028c.com
- * 更新: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();
- }
- } // 類結束
- ?>
新聞熱點
疑難解答