Codeigniter框架實現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法
更新時間:2014年12月05日 11:06:34 投稿:shichen2014
這篇文章主要介紹了Codeigniter框架實現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法,實現(xiàn)了對獲取當前頁的數(shù)據(jù)和總條數(shù)方法的封裝,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了Codeigniter框架實現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
一般在數(shù)據(jù)分頁的時候需要獲取當前頁的數(shù)據(jù)和總條數(shù),一般人是在model中封裝兩個函數(shù)分別獲取當前頁的數(shù)據(jù)和數(shù)據(jù)總條數(shù),業(yè)務(wù)邏輯類似,感覺有點冗余,可以封裝在一起
復制代碼 代碼如下:
/**
* 獲取分頁數(shù)據(jù)及總條數(shù)
* @param string @tablename 表名
* @param mixed $where 條件
* @param int $limit 每頁條數(shù)
* @param int $offset 當前頁
*
*/
public function get_page_data($tablename, $where, $limit, $offset, $order_by, $db)
{
if(empty($tablename))
{
return FALSE;
}
$dbhandle = empty($db) ? $this->db : $db;
if($where)
{
if(is_array($where))
{
$dbhandle->where($where);
}
else
{
$dbhandle->where($where, NULL, false);
}
}
$db = clone($dbhandle);
$total = $dbhandle->count_all_results($tablename);
if($limit)
{
$db->limit($limit);
}
if($offset)
{
$db->offset($offset);
}
if($order_by)
{
$db->order_by($order_by);
}
$data = $db->get($tablename)->result_array();
return array('total' => $total, 'data' => $data);
}
* 獲取分頁數(shù)據(jù)及總條數(shù)
* @param string @tablename 表名
* @param mixed $where 條件
* @param int $limit 每頁條數(shù)
* @param int $offset 當前頁
*
*/
public function get_page_data($tablename, $where, $limit, $offset, $order_by, $db)
{
if(empty($tablename))
{
return FALSE;
}
$dbhandle = empty($db) ? $this->db : $db;
if($where)
{
if(is_array($where))
{
$dbhandle->where($where);
}
else
{
$dbhandle->where($where, NULL, false);
}
}
$db = clone($dbhandle);
$total = $dbhandle->count_all_results($tablename);
if($limit)
{
$db->limit($limit);
}
if($offset)
{
$db->offset($offset);
}
if($order_by)
{
$db->order_by($order_by);
}
$data = $db->get($tablename)->result_array();
return array('total' => $total, 'data' => $data);
}
希望本文所述對大家基于Codeigniter框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP CURL或file_get_contents獲取網(wǎng)頁標題的代碼及兩者效率的穩(wěn)定性問題
PHP CURL與file_get_contents函數(shù)都可以獲取遠程服務(wù)器上的文件保存到本地,但在性能上面兩者完全不在同一個級別,下面通過一個例子給大家介紹PHP CURL或file_get_contents獲取網(wǎng)頁標題的代碼及兩者效率的穩(wěn)定性問題,需要的朋友參考下2015-11-11

