【问题标题】:codeigniter pagination for a query查询的codeigniter分页
【发布时间】:2010-11-24 01:30:27
【问题描述】:

到目前为止,找到了很多帮助来使分页适用于 get(table) 命令。

我需要的是从几个基于 sql where 语句的链接表中只选择几个条目。

我猜query 命令是要使用的命令,但在这种情况下,我该如何进行分页,因为该命令不需要额外的参数,例如$config['per_page']

感谢您的帮助

【问题讨论】:

    标签: sql codeigniter foreign-keys pagination


    【解决方案1】:

    没有更多信息可以继续,我认为您正在寻找的内容类似于以下内容。

    public function pagination_example($account_id)
    {
        $params = $this->uri->ruri_to_assoc(3, array('page'));
    
        $where = array(
            'account_id'    => $account_id,
            'active'        => 1
        );
    
        $limit = array(
            'limit'     => 10,
            'offset'    => (!empty($params['page'])) ? $params['page'] : 0
        );
    
        $this->load->model('pagination_model');
        $data['my_data'] = $this->pagination_model->get_my_data($where, $limit);
    
        foreach($this->uri->segment_array() as $key => $segment)
        {
            if($segment == 'page')
            {
                $segment_id = $key + 1;
            }
        }
    
        if(isset($segment_id))
        {
            $config['uri_segment'] = $segment_id;
        }
        else 
        {
            $config['uri_segment'] = 0;
        }
    
        $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/controller_name/method_name/whatever_your_other_parameters_are/page/';              
        $config['total_rows'] = $this->pagination_model->get_num_total_rows();// Make a method that will figure out the total number
        $config['per_page']     = '10';
    
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
    
        $this->load->view('pagination_example_view', $data);
    
    }
    
        // pagination_model
    public function get_my_data($where = array(), $limit = array())
    {
        $this->db
            ->select('whatever')
            ->from('wherever')
            ->where($where)
            ->limit($limit['limit'], $limit['offset']);
        $query = $this->db->get();
    
        if($query->num_rows() > 0)
        {
            $data = $query->result_array();
            return $data;
        }
        return FALSE;
    }
    

    这至少应该让你走上正轨 如果这不是你要问的,如果你能更具体一点,我很乐意提供更多帮助。你的一些代码怎么样。

    【讨论】:

    • 我最大的问题是如何设置 $config['total_rows'] ;我觉得我必须查询数据库两次:一次没有限制(获取 $config['total_rows'] ),第二次限制显示。听起来效率低下。是这样吗?
    【解决方案2】:

    我能想到的唯一其他选择是在您的 select 语句中编码一个计数或不限制查询并使用 array_slice 选择返回数组的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2015-07-03
      相关资源
      最近更新 更多