background image

php mysql 分页类与调用实例

本文分享一段

php 代码,一个 php 与 mysql 的分页类,附有调用示例,有需要的朋友,可

以作个参考。
本节分享一个

php、mysql 分页类,代码如下:

<?php 
/**** 
文件名

 : pagination.class.php 

 能 : php mysql 

 

分页类

****/ 
class pagination { 

    var $fullresult;    // 数据库中整个记录的结果集
    var $totalresult;   // 总记录数
    var $query;         // 用户查询
    var $resultPerPage; // 

 

每页显示的记录数

    var $resultpage;    // 

 

每一页的记录集

    var $pages;     // 

 

总页数

    var $openPage;  // 当前页
     
/* 
@param - 查询语句
@param - 

 

每页记录数

*/ 
    function createPaging($query,$resultPerPage)  
    { 
        $this->query        =    $query; 
        $this->resultPerPage=    $resultPerPage; 
        $this->fullresult    =    mysql_query($this->query); 
        $this->totalresult    =    mysql_num_rows($this->fullresult); 
        $this->pages        =    $this->findPages($this->totalresult,$this->resultPerPage); 
        if(isset($_GET['page']) && $_GET['page']>0) { 
            $this->openPage    =    $_GET['page']; 
            if($this->openPage > $this->pages) { 
                $this->openPage    =    1; 
            } 
            $start    =    $this->openPage*$this->resultPerPage-$this->resultPerPage; 
            $end    =    $this->resultPerPage; 
            $this->query.=    " LIMIT $start,$end"; 
        } 
        elseif($_GET['page']>$this->pages) { 
            $start    =    $this->pages; 
            $end    =    $this->resultPerPage; 
            $this->query.=    " LIMIT $start,$end";