Saturday, 14 September 2013

Codeigniter pagination ignores last db entries

Codeigniter pagination ignores last db entries

So I have code like this Controller:
function latest()
{
// Pagination $config
$config['base_url'] = base_url() . 'posts/latest';
$config['total_rows'] = $this->m_posts->getLatestPostsCount(50);
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['posts'] = $this->m_posts->getLatestPosts($config['per_page'],
$page);
$data['pagination'] = $this->pagination->create_links();
$this->template->build('posts/posts', $data);
}
and model:
function getLatestPosts($limit = 0, $start = 0)
{
$this->db->order_by('id', 'desc');
// Get LATEST posts -> pagination
$q = $this->db->get('posts', $limit, $start);
foreach ($q->result() as $key => $row)
{
$data[$key]['id'] = $row->id;
// and so on
}
return $data;
}
function getLatestPostsCount($limit = 0)
{
$this->db->order_by('id', 'desc');
$q = ($limit == 0) ? $this->db->get('posts') : $this->db->get('posts',
$limit);
return $q->num_rows();
}
so yeah.. the problem is that if I have, lets say, 50 db entries (posts)
and set per page = 8. it is 50/8 = 6, right? I have problem with
pagination that those 2 just dissapears... codeigniter just don't create
the 7th page with last two entries.... How can I get this 7th page, and
display 2 missing posts???

No comments:

Post a Comment