Afshin
Afshin

Reputation: 2437

use_page_numbers in codeigniter pagination class?

I have a problem with using use_page_numbers config set to true in my pagination class! When I click on the link of page 2, the number of rows it retrieves from database is correct, but the problem is that: the first row of page 2 is the third row of page one ! It means that page 2 starts with the same row from the database which has been retrieved in the first page in the third row. for example :

Page 1: 10, 11, 12, 13, 14

Page 2: 12, 13, 14, 15, 16

and of course the page 3 starts from the second row of page 2 :

Page 3: 13, 14, 15, 16, 17

This is the code I have :

function get_brands_list($options = array())
{
    //Pagination config
    $config['base_url'] = base_url() . 'admin/brands/page/';
    $config['total_rows'] = $this->db->get('mg_brands')->num_rows();
    $config['per_page'] = 5;
    $config['num_links'] = 4;
    $config['uri_segment'] = 4;
    $config['use_page_numbers'] = TRUE;

    $this->pagination->initialize($config);

    $offset = $this->uri->segment(4, 0);
    $this->db->order_by('brand_Sort', 'ASC');
    $query = $this->db->get('mg_brands', $config['per_page'], $offset);

    if(isset($options['brand_Id']) || isset($options['brand_Name']))
        return $query->row(0);

    return $query->result();
} 

Upvotes: 0

Views: 2787

Answers (1)

Code Prank
Code Prank

Reputation: 4250

You have a problem in calculation of offset variable.... Try this one:

$page_num = $this->uri->segment(4, 0);

$offset = ($page_num - 1) * $config['per_page'];

Upvotes: 3

Related Questions