CodeIgniter Forums

Full Version: use_page_numbers problem in pagination class !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]afshin[/eluser]
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 :

Code:
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();
}

El Forum

[eluser]afshin[/eluser]
Any suggestion?! what is wrong with my code ?!

El Forum

[eluser]Aken[/eluser]
Your offset is wrong. You're going by the page number only (1, 2, 3, etc...). You need to go by (page number - 1) * per page.

If you have five articles per page, your offsets would be (page # - offset):
1 - 0
2 - 5
3 - 10
4 - 15
etc...

El Forum

[eluser]afshin[/eluser]
Thanks. it really helped Smile