欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

fleaphp常用方法分页之Pager使用方法

程序员文章站 2022-06-03 10:41:53
pager 分页函数 复制代码 代码如下: /** * 构造函数 * * 如果 $source 参数是一个 tabledatagateway 对象,则 flea_helpe...
pager 分页函数
复制代码 代码如下:

/**
* 构造函数
*
* 如果 $source 参数是一个 tabledatagateway 对象,则 flea_helper_pager 会调用
* 该 tdg 对象的 findcount() 和 findall() 来确定记录总数并返回记录集。
*
* 如果 $source 参数是一个字符串,则假定为 sql 语句。这时,flea_helper_pager
* 不会自动调用计算各项分页参数。必须通过 setcount() 方法来设置作为分页计算
* 基础的记录总数。
*
* 同时,如果 $source 参数为一个字符串,则不需要 $conditions 和 $sortby 参数。
* 而且可以通过 setdbo() 方法设置要使用的数据库访问对象。否则 flea_helper_pager
* 将尝试获取一个默认的数据库访问对象。
*
* @param tabledatagateway|string $source
* @param int $currentpage
* @param int $pagesize
* @param mixed $conditions
* @param string $sortby
* @param int $basepageindex
*
* @return flea_helper_pager
*/
function flea_helper_pager(& $source, $currentpage, $pagesize = 20, $conditions = null, $sortby = null, $basepageindex = 0)
{
$this->_basepageindex = $basepageindex;
$this->_currentpage = $this->currentpage = $currentpage;
$this->pagesize = $pagesize;
if (is_object($source)) {
$this->source =& $source;
$this->_conditions = $conditions;
$this->_sortby = $sortby;
$this->totalcount = $this->count = (int)$this->source->findcount($conditions);
$this->computingpage();
} elseif (!empty($source)) {
$this->source = $source;
$sql = "select count(*) from ( $source ) as _count_table";
$this->dbo =& flea::getdbo();
$this->totalcount = $this->count = (int)$this->dbo->getone($sql);
$this->computingpage();
}
}

pager 参数说明
$source 数据库操作类
$currentpage 当前页
$pagesize 每页显示记录数量
$conditions 查询条件
$sortby 排序方式
$basepageindex 页码基数
pager 使用示例(实例)
复制代码 代码如下:

$dirname = dirname(__file__);
define('app_dir', $dirname . '/app');
define('no_legacy_fleaphp', true);
require($dirname.'/fleaphp/flea/flea.php');
//设置缓存目录
flea::setappinf('internalcachedir',$dirname.'/_cache');
//链接数据库
$dsn = array(
'driver' => 'mysql',
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'wordpress'
);
flea::setappinf('dbdsn',$dsn);
//读取wp_posts的内容
flea::loadclass('flea_db_tabledatagateway');
flea::loadclass('flea_helper_pager');
//flea::loadhelper('pager');
class teble_class extends flea_db_tabledatagateway {
var $tablename = 'wp_posts';
var $primarykey = 'id';
}
$tableposts =& new teble_class();
$pager =& new flea_helper_pager($tableposts,2,5);
$page = $pager->getpagerdata();
print_r($page);

getpagerdata 返回一些数据供调用
复制代码 代码如下:

$data = array(
'pagesize' => $this->pagesize,
'totalcount' => $this->totalcount,
'count' => $this->count,
'pagecount' => $this->pagecount,
'firstpage' => $this->firstpage,
'firstpagenumber' => $this->firstpagenumber,
'lastpage' => $this->lastpage,
'lastpagenumber' => $this->lastpagenumber,
'prevpage' => $this->prevpage,
'prevpagenumber' => $this->prevpagenumber,
'nextpage' => $this->nextpage,
'nextpagenumber' => $this->nextpagenumber,
'currentpage' => $this->currentpage,
'currentpagenumber' => $this->currentpagenumber,
);