Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php mysql分页类与调用实例

php mysql分页类与调用实例

发布时间:2016-12-09   编辑:www.jquerycn.cn
本文分享一段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"; 
        } 
        else { 
            $this->openPage    =    1; 
            $this->query .=    " LIMIT 0,$this->resultPerPage"; 
        } 
        $this->resultpage =    mysql_query($this->query); 
    } 
/* 
@param - 总记录数
@param - 每页记录数 
*/ 
    function findPages($total,$perpage)  
    { 
        $pages    =    intval($total/$perpage); 
        if($total%$perpage > 0) $pages++; 
        return $pages; 
    } 
     
/* 
显示分页
*/ 
    function displayPaging()  
    { 
        $self    =    $_SERVER['PHP_SELF']; 
        if($this->openPage<=0) { 
            $next    =    2; 
        } 

        else { 
            $next    =    $this->openPage+1; 
        } 
        $prev    =    $this->openPage-1; 
        $last    =    $this->pages; 

        if($this->openPage > 1) { 
            echo "<a href=$self?page=1>First</a>&nbsp&nbsp;"; 
            echo "<a href=$self?page=$prev>Prev</a>&nbsp&nbsp;"; 
        } 
        else { 
            echo "First&nbsp&nbsp;"; 
            echo "Prev&nbsp&nbsp;"; 
        } 
        for($i=1;$i<=$this->pages;$i++) { 
            if($i == $this->openPage)  
                echo "$i&nbsp&nbsp;"; 
            else 
                echo "<a href=$self?page=$i>$i</a>&nbsp&nbsp;"; 
        } 
        if($this->openPage < $this->pages) { 
            echo "<a href=$self?page=$next>Next</a>&nbsp&nbsp;"; 
            echo "<a href=$self?page=$last>Last</a>&nbsp&nbsp;"; 
        } 
        else { 
            echo "Next&nbsp&nbsp;"; 
            echo "Last&nbsp&nbsp;"; 
        }     
    } 
} 
?>

调用示例:

<?php 
/*** 
文件名称 : showresult.php
功 能 : 显示分页结果
***/ 
include_once("pagination.class.php");  // 调用分页类
$pagination    =    new pagination(); 
$query    =    "SELECT * FROM `student` WHERE 1"; // 查询语句
/* 
调用方法,创建分页 
@param - 数据库查询
@param - 每页显示的记录数 
*/ 
$pagination->createPaging($query,10); 
echo '<table border="1" width="400" align="center">'; 
while($row=mysql_fetch_object($pagination->resultpage)) { 
    echo '<tr><td>'.$row->name.'</td><td>'.$row->age.'</td></tr>'; // display name and age from database 
} 
echo '</table>'; 
echo '<table border="1" width="400" align="center">'; 
echo '<tr><td>'; 
$pagination->displayPaging(); 
echo '</td></tr>'; 
echo '</table>'; 
?>

您可能感兴趣的文章:
入门级PHP简单分页代码
php分页代码实例有注释
php mysql分页类与调用实例
php数据库连接、查询、显示结果的小例子
php操作数据库的简单示例
select语句实现mysql分页查询
mysql 分页查询的sql语句
mysql字段、主键、索引等的创建与修改命令
php生成翻页链接列表的函数举例
php session操作类(附实例)

关键词: mysql分页  php分页  分页代码   
[关闭]