CI分页器pagination的原理及实现

以下是本人原创,如若转载和使用请注明转载地址。本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址
下面这段代码是从官网上翻译过来的,介绍了分页的用例
1
2
3
4
5
6
7
8
9
10
11
12
13
public function list()
{
$this->load->library('pagination');//加载分页类
$config['base_url'] = base_url().'index.php/main/list';//设置基地址
$config['uri_segment']=3;//设置url上第几段用于传递分页器的偏移量
$config['total_rows'] = $this->db->count_all('db_list');//自动从数据库中取得total_row信息
$config['per_page'] = 10; //每页显示的数据数量
$this->pagination->initialize($config); //设置完成分页器
$this->load->library('table');//加载表格类
$query=$this->db->get('my_list',$config['per_page'],$this->uri->segment(3));//这一行代码是关键!是pagination与table结合的关键.per_page告诉此次sql查询限制数量,uri_segment告诉此次查询的偏移量(从哪一行数据开始查询).
echo $this->table->generate($query);//显示查询到的数据
echo $this->pagination->create_links();//显示分页器
}

可以看出其中使用到了一些配置文件,在pagination.php文件中 ​下面我们看看这个文件的详细内容,如果你的文件和这个不同的话,可以复制进去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
define('PER_PAGE', 1);
 
$config['per_page'] = PER_PAGE;
$config['num_links'] = 2;
$config['first_link'] = "首页";
$config['last_link'] = "末页";
$config['first_tag_open'] = '<div>';
$config['first_tag_close'] = '</div>';
$config['last_tag_open'] = '<div>';
$config['last_tag_close'] = '</div>';

之后才是我们的正式的核心内容

这个是页面的链接,用于显示首次加载的情形。

1
<li><a href="<?=base_url();?>index.php/admin/info/showAll" target="mainFrame"><span>信息管理</span></a></li>

之后我们就需要到控制层去找showAll函数了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public function showAll(){
        $table = 'ts_product';
        $num = $this->traceInfo_model->getNumByTable($table);
//      echo $num;
//      $arr['totalNum'] = $num;
        $offset = $this->uri->segment(4);
        $arr['traceData'] = $this->getTraces($offset);
        //使用分页器进行分页
        $config['base_url'] = base_url().'index.php/admin/info/showAll';//设置基地址
        $config['uri_segment']=4;//设置url上第几段用于传递分页器的偏移量
        $config['total_rows'] = $num;//自动从数据库中取得total_row信息
        $config['per_page'] = 1; //每页显示的数据数量
        $this->pagination->initialize($config); //设置完成分页器
        $arr['page'] = $this->pagination->create_links();
 
        $this->load->view('admin/subject/information_show_all',$arr);
    }

其中showAll函数调用了getTraces($offset)函数,用于获取每次我们点击页面时的不同的页,下面是此函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 分页获取全部课题信息
       public function getTraces($offset){
           $data = array();
           //调用model层的获取数据的函数
           $result = $this->traceInfo_model->getTracesTable($data, PER_PAGE, $offset);
 
           foreach ($result as $r){
               $arr = array(
                   'product_tracecode' => $r->product_tracecode,
                   'product_name' => $r->product_name,
                   'product_inputtime' => $r->product_inputtime,
                   'product_inputuser' => $r->product_inputuser
                   );
               array_push($data, $arr);
           }
           return $data;
       }

此时我们会去调用model层的函数,去获得数据库的数据,其中$this->traceInfo_model->getTracesTable就是调用model层的函数。

1
2
3
4
5
6
7
8
9
/**
 * 处理分页的函数
 */
function getTracesTable($array, $per_page, $offset){
    $this->db->select();
    $this->db->where($array);
    $q = $this->db->get('ts_product', $per_page, $offset);
    return $q->result();
}

这个函数用的是CI框架提供的数据库操作语句,其中$this->db->get('ts_product'$per_page$offset);第一个参数就是我们数据库中的表名称,其他的都很好理解。最难理解的是offset表示从哪一行数据开始查询。


这里我所不知道的是 如果有两个表关联,怎么办呢?我们还需要使用这个CI提供的数据库操作语句吗?也就是这里的where该怎么写呢?请教请教啊???




感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址,并且请尊重劳动成果,谢谢!







posted @ 2013-12-26 19:48  Tim&Blog  阅读(1149)  评论(0编辑  收藏  举报