php专区

 首页 > php专区 > PHP应用 > php类库 > 一个分页显示类 - php类库

一个分页显示类 - php类库

分享到:
【字体:
导读:
          ?php * *分页显示类 *PageItem php v1 0 0 *编程:Bobanboban@21php com *讨论:http: www phpfensi com*更新:2004-02-02 *说...

一个分页显示类

  1. /*
  2.  * 分页显示类  
  3.  * PageItem.php v 1.0.0  
  4.  * 编程:Boban  
  5.  * 讨论:http://www.phpfensi.com  
  6.  * 更新:2004-02-02  
  7.  * 说明:  
  8.  * 1. 配合MYSQL数据库使用  
  9.  * 2. 类没有提供连接数据库的功能,需在外部建立数据库连接。  
  10.  * */ 
  11. /*
  12.  * 使用方法:  
  13.  * $sql = "select * from news limit 0,10";  
  14.  * $hdc = new PageItem($sql);  
  15.  * echo $hdc->myPageItem();  
  16.  * $arrRecords = $hdc->ReadList();  
  17.  * */ 
  18. if (!defined("__PAGEITEM__")) {  
  19.     define("__PAGEITEM__", 1);  
  20. else {  
  21.     exit(3);  
  22. }  
  23. class PageItem {  
  24.     var $iDefaultRecords = 10; // 默认每页显示记录数,如果没有设置,就使用默认值  
  25.     var $iMaxRecord//每页记录数  
  26.     var $iTotal//记录总数  
  27.     var $sqlRecord// 获取记录的SQL查询  
  28.     var $iPages//总页数  
  29.     var $CPages//当前页数  
  30.     /*
  31.   * 构造函数 -- 初始化变量  
  32.   * 参数:SQL查询语句,将忽略LIMIT语句  
  33.   * */ 
  34.     function PageItem($sql = "")  
  35.     {   
  36.         // register_shutdown_function($this->_PageItem());  
  37.         $this->SetMaxRecord($this->iDefaultRecords);  
  38.         /*
  39.        * 解析SQL语句  
  40.        * */ 
  41.         if ($sql <> "") {  
  42.             list($sql) = spliti("LIMIT"$sql); // 去除LIMIT语句  
  43.             $this->sqlRecord = trim($sql);  
  44.             list(, $sql) = spliti("FROM"$sql);  
  45.             $sql = trim($sql);  
  46.    if(preg_match ("/bGROUPb bBYb/i"$sql))  
  47.    {  
  48.     //echo "HAVE GROUP BY";  
  49.     if(preg_match ("/bHAVINGb/i"$sql)) list(,$field) = spliti("HAVING",$sql);  
  50.     list($field) = spliti(' ',trim($field));  
  51.     //echo $field;  
  52.     $this->iTotal = $this->CountRecord("SELECT $field,COUNT(DISTINCT $field) AS cnt FROM " . $sql,2);  
  53.    }  
  54.    else $this->iTotal = $this->CountRecord("SELECT COUNT(*) AS cnt FROM " . $sql,1);  
  55.         }   
  56.         $this->iPages = ceil($this->iTotal / $this->iMaxRecord);  
  57.         $this->CPages = $_REQUEST['page'];  
  58.         if ($this->CPages <= 0) $this->CPages = 1;  
  59.         if ($this->CPages > $this->iPages) $this->CPages = $this->iPages;  
  60.         //echo "SELECT COUNT(*) AS cnt FROM " . $sql;  
  61.   //echo $this->iTotal;  
  62.     }   
  63.     /*
  64.      * 析构函数 -- 暂时不可用  
  65.      * */ 
  66.     function _PageItem()  
  67.     {   
  68.         // $this->linkid = NULL;  
  69.     }  
  70.     function SetMaxRecord($cnt)  
  71.     {  
  72.         $this->iMaxRecord = $cnt;  
  73.     }  
  74.     /*
  75.      * 统计匹配的记录总数  
  76.      * */ 
  77.     function CountRecord($sql,$type)  
  78.     {  
  79.   //echo $sql;  
  80.   if($type == 1)  
  81.   {  
  82.    if (($records = mysql_query($sql)) && ($record = mysql_fetch_assoc($records))) {  
  83.     return $record['cnt'];  
  84.    } else return 0;  
  85.   }  
  86.   elseif($type == 2)  
  87.   {  
  88.    if($records = mysql_query($sql))  
  89.     return mysql_affected_rows();  
  90.   }  
  91.     }  
  92.  /* 
  93.   * 读取记录  
  94.   * */ 
  95.  function ReadList()  
  96.  {  
  97.   $ret = array();  
  98.   $this->sqlRecord.=" LIMIT ".($this->CPages-1)*$this->iMaxRecord.",".$this->iMaxRecord;  
  99.   $records = mysql_query($this->sqlRecord);  
  100.   if(!$recordsreturn;  
  101.   while($record = mysql_fetch_array($records))  
  102.   {  
  103.    $ret[] = $record;  
  104.   }  
  105.   return $ret;  
  106.  }  
  107.     function LinktoPage($page$msg)  
  108.     {  
  109.         $link = $this->PageUrl($page);  
  110.         return "$msgn";  
  111.     }   
  112.     function PageUrl($page)  
  113.     {  
  114.         $phpself = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];  
  115.         $querystring = $_SERVER['QUERY_STRING'];  
  116.         $querystring = preg_replace("/page=[0-9]*&?/i"""$querystring);  
  117.         $link = $phpself . "?page=$page&" . $querystring;  
  118.         return $link;  
  119.     }  
  120.     /* 
  121.   * 显示当前页及总页数     
  122.   * */ 
  123.     function PageNav()  
  124.     {  
  125.         echo "第" . $this->CPages . "页/共" . $this->iPages . "页";  
  126.     }  
  127.     /* 
  128.    * 显示翻页按钮,包括首页、下页、上页、未页  
  129.    * */ 
  130.     function PageButton()  
  131.     {  
  132.         if ($this->CPages > 1) {  
  133.             echo $this->LinktoPage(1, "首页");  
  134.             echo " | ";  
  135.             echo $this->LinktoPage($this->CPages-1, "上一页");  
  136.         } else {  
  137.             echo "首页 | 上一页";  
  138.         }  
  139.         if ($this->CPages < $this->iPages) {  
  140.             echo " | ";  
  141.             echo $this->LinktoPage($this->CPages + 1, "下一页");  
  142.             echo " | ";  
  143.             echo $this->LinktoPage($this->iPages, "首页");  
  144.         } else {  
  145.             echo " | 下一页 | 尾页";  
  146.         }   
  147.     }  
  148.     /* 
  149.    * 显示跳转页选择框  
  150.    * */ 
  151.     function SelectItem()  
  152.     {  
  153.         echo "跳到第n";  
  154.         for($i = 1;$i <= $this->iPages;$i++) {  
  155.             if ($this->CPages == $i)  
  156.                 $extra = "selected";  
  157.             else 
  158.                 $extra = "";  
  159.             echo " . $this->PageUrl($i) . "' $extra>$i";  
  160.         }   
  161.         echo "n";  
  162.     }  
  163.     /*
  164.      * 一次性显示所有按钮组件  
  165.      * */ 
  166.     function myPageItem()  
  167.     {  
  168.         $this->PageButton();  
  169.         $this->SelectItem();  
  170.         $this->PageNav();  
  171.     }   
  172. // 类结束  
  173. ?> 
分享到:
新手写的分页类 - php类库
新手写的分页类 刚学PHP,写的不好,请大家别笑,用Mysql数据库,我还不知道在类中怎么捕捉错误,然后返回。 代码如下: [/php]     应用,假设已经建立了test表,并且已经连上   [php]$pages = new Pageslice;//建立分页对象   $pages->bindTable(&#039;test&#039;);/...
分享的一个分页类 - php类库
分享的一个分页类  
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……