Codeigniter框架实现获取分页数据和总条数

作者:袖梨 2022-06-25
代码如下 复制代码
/**
* 获取分页数据及总条数
* @param string @tablename 表名
* @param mixed $where 条件
* @param int $limit 每页条数
* @param int $offset 当前页
*
* @link http://www.1*11*cn*n.net
代码如下 复制代码
*/
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);
}

相关文章

精彩推荐