本文實(shí)例講述了Codeigniter框架實(shí)現(xiàn)獲取分頁(yè)數(shù)據(jù)和總條數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
一般在數(shù)據(jù)分頁(yè)的時(shí)候需要獲取當(dāng)前頁(yè)的數(shù)據(jù)和總條數(shù),一般人是在model中封裝兩個(gè)函數(shù)分別獲取當(dāng)前頁(yè)的數(shù)據(jù)和數(shù)據(jù)總條數(shù),業(yè)務(wù)邏輯類似,感覺(jué)有點(diǎn)冗余,可以封裝在一起
* 獲取分頁(yè)數(shù)據(jù)及總條數(shù)
* @param string @tablename 表名
* @param mixed $where 條件
* @param int $limit 每頁(yè)條數(shù)
* @param int $offset 當(dāng)前頁(yè)
*
*/
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);
}
希望本文所述對(duì)大家基于Codeigniter框架的PHP程序設(shè)計(jì)有所幫助。