Maison > php教程 > php手册 > le corps du texte

Codeigniter(CI)框架分页函数及相关知识

WBOY
Libérer: 2016-06-06 20:17:52
original
1259 Les gens l'ont consulté

文章主要介绍了一个自己封装的Codeigniter(CI)框架的分页函数以及Codeigniter(CI)框架分页类的使用心得,非常简单实用,希望对大家能有所帮助

一般在数据分页的时候需要获取当前页的数据和总条数,一般人是在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);
    }

CI框架分页类使用心得

CI分页的url地址有四种方式
a) locahost/news/page/2 这个2表示第二页
b) localhost/news/page/20 这个20表示从第20条记录开始分页,即页面的第一条记录,是数据库中的第20条记录。
c) localhost/news?per_page=2 第二页
d) localhost/news?per_page=20 同b)

首先我们先看一下CI分页的参数:

复制代码 代码如下:


$config['base_url'] = $url;  
/* 分页的基础 URL
如果你想用a、b的链接形式,则该url应该形式如/news/page/ 
如果链接是c、d的形式,则url应该如/news? 
*/ 
$config['total_rows'] = $total;//记录总数,这个没什么好说的了,就是你从数据库取得记录总数  
$config['per_page'] = $pagesize; //每页条数。额,这个也没什么好说的。。自己设定。默认为10好像。  
$config['page_query_string'] = TRUE;  
/*传参形式。开启true则会自动在你的url后面加上&per_page=3。(这个per_page是默认的查询字符,当然你也可以用$config['query_string_segment']来自己设定)
因此c、d中的形式一般是为localhost/news?&per_page=2不过都一样,没什么影响。get的per_page还是3 
*/ 
$config['first_link'] = '首页'; // 第一页显示  
$config['last_link'] = '末页'; // 最后一页显示  
$config['next_link'] = '下一页 >'; // 下一页显示  
$config['prev_link'] = ' $config['cur_tag_open'] = ' '; // 当前页开始样式  
$config['cur_tag_close'] = '
';  
/*当前页结束样式。这些你可以自己尝试一下。
比如说我想让当前页的分页数字样式好看一点,红色字体等。你就可以在current上加上css代码 
*/ 
$config['num_links'] = 2;// 当前连接前后显示页码个数。意思就是说你当前页是第5页,那么你可以看到3、4、5、6、7页。  
$config['uri_segment'] = 4;  
/*这个是你在用a)、b)链接样式的时候,用来判断页页数。
比如localhost/news/page/3  这个uri_segment就要设定为3。localhost/news/title/page/3这个就要设定为4 
*/ 
$config['use_page_numbers'] = TRUE;  
/*这个就是a)、b)的差别了。开启了,page就会表示页数。false就会表示记录数
*/ 

刚开始在网上查资料的时候,有很多这种写法。

复制代码 代码如下:


$this->model->get_news($config['per_page'],$this->uri->segment(3)); 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!