开发者

codeigniter pagination for a query

开发者 https://www.devze.com 2023-01-27 04:23 出处:网络
So far found plenty of help to get the pagination working for a get(table) command. What I need is to pick only few of the entries from a couple of linked tables based on a sql where statement.

So far found plenty of help to get the pagination working for a get(table) command.

What I need is to pick only few of the entries from a couple of linked tables based on a sql where statement.

I guess the query command is the one to use but in this case how do I do the pagination since that command does not take extra parameters su开发者_如何学JAVAch $config['per_page']

Thanks for the help


Without any more info to go on I think that what you're looking for is something like the following.

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;
}

This should at least get you on the right track If this isn't what you're asking I'd happy to help more if you can be a little more specific. How about some of your code.


The only other options that I can think of would be to either code a count in your select statement or not limit the query and use array_slice to select a portion of the returned array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号