程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> fleaphp常用方法分頁之Pager使用方法

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

編輯: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,
);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved