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

yipeiwu_com6年前PHP代码库
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,
);

相关文章

PHP实现找出有序数组中绝对值最小的数算法分析

本文实例讲述了PHP实现找出有序数组中绝对值最小的数算法。分享给大家供大家参考,具体如下: 问题: 一个有序数组,值有可能有负值,也有可能没有,现需要找出其中绝对值最小的值。 方法1:...

PHP中将一个字符串部分字符用星号*替代隐藏的实现代码

有时候我们在开发中会遇到这样一种情况,例如:显示手机号我们需要将中间4位遮挡掉,一般使用“*”号代替,或是显示身份证号码是为了保护个人信息也同样需要遮挡掉4位,故可用到下列方式、代码进行...

php如何比较两个浮点数是否相等详解

前言 本文主要给大家介绍了关于利用php如何比较浮点数是否相等的相关内容,下面话不多说了,来一起看看详细的介绍吧 看下面这段代码, 0.9+0.1 的相加结果与 1 进行比较 &...

PHP中使用Memache作为进程锁的操作类分享

<?php // 使用Memache 作为进程锁 class lock_processlock{ // key 的前缀 protected $sLoc...

discuz图片顺序混乱解决方案

discuz图片顺序混乱解决方案

说明 discuz在发表帖子的时候,添加多张图片,然后直接发表帖子,图片顺序有时候会乱掉 即使上传图片窗口中图片顺序正确,发布之后还是会乱掉 分析 看url,程序代码中看不出什么 将图片...