Home > Backend Development > PHP Tutorial > Codeigniter框架实现获取分页数据和总条数的方法_php实例

Codeigniter框架实现获取分页数据和总条数的方法_php实例

WBOY
Release: 2016-06-07 17:14:54
Original
784 people have browsed it

本文实例讲述了Codeigniter框架实现获取分页数据和总条数的方法。分享给大家供大家参考。具体实现方法如下:

一般在数据分页的时候需要获取当前页的数据和总条数,一般人是在model中封装两个函数分别获取当前页的数据和数据总条数,业务逻辑类似,感觉有点冗余,可以封装在一起

复制代码 代码如下:
/**
* 获取分页数据及总条数
* @param string @tablename 表名
* @param mixed $where 条件
* @param int $limit 每页条数
* @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程序设计有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template