一个分页显示类
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (!defined("__PAGEITEM__")) {
- define("__PAGEITEM__", 1);
- } else {
- exit(3);
- }
- class PageItem {
- var $iDefaultRecords = 10;
- var $iMaxRecord;
- var $iTotal;
- var $sqlRecord;
- var $iPages;
- var $CPages;
-
-
-
-
- function PageItem($sql = "")
- {
-
- $this->SetMaxRecord($this->iDefaultRecords);
-
-
-
- if ($sql <> "") {
- list($sql) = spliti("LIMIT", $sql);
- $this->sqlRecord = trim($sql);
- list(, $sql) = spliti("FROM", $sql);
- $sql = trim($sql);
- if(preg_match ("/\bGROUP\b \bBY\b/i", $sql))
- {
-
- if(preg_match ("/\bHAVING\b/i", $sql)) list(,$field) = spliti("HAVING",$sql);
- list($field) = spliti(' ',trim($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;
-
-
- }
-
-
-
- function _PageItem()
- {
-
- }
- function SetMaxRecord($cnt)
- {
- $this->iMaxRecord = $cnt;
- }
-
-
-
- function CountRecord($sql,$type)
- {
-
- 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();
- }
- }
- ?>
|