Razvan Zamfir
Razvan Zamfir

Reputation: 4684

Codeigniter 3 blog bug: pagination shows the first page only

I am working on a basic blog application in Codeigniter 3.1.8.

The posts are filtered by category by this method from the Posts_model model:

public function get_posts_by_category($id, $limit, $offset) {
    $this->db->order_by('posts.id', 'DESC');
    $this->db->join('categories', 'categories.id = posts.cat_id');
    $query = $this->db->get_where('posts', array('cat_id' => $id));
    return $query->result();
}

In the Categories controller I have:

public function posts($id) {
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url('/categories/posts/' . $id),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 12,
        'total_rows' => $this->Posts_model->get_num_rows_by_category($id),
        'uri_segment' => 3,
        'first_link' => '«',
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_link' => '&raquo;',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_link' =>  '&rsaquo;',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_link' => '&lsaquo;',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><span>',
        'cur_tag_close' =>  '</span></li>'
    ];
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);

    $data['categories'] = $this->Categories_model->get_categories();
    $data['category_name'] = $this->Categories_model->get_category($id)->name;
    $data['posts'] = $this->Posts_model->get_posts_by_category($id, $limit, $offset);

    $this->load->view('partials/header', $data);
    $this->load->view('categories/posts');
    $this->load->view('partials/footer');
}

The problem: whenever I display a category, I can only see the post on the first page, at the url: http://localhost/blog/categories/posts/1 even if I click different pagination items and the url shows http://localhost/blog/categories/posts/1?page=2 and so on.

Where is my mistake?

Upvotes: 2

Views: 314

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

You dont add limit in model's get_posts_by_category method. Try this

public function get_posts_by_category($id, $limit, $offset) {
    $this->db->order_by('posts.id', 'DESC');
    //you misssed this line 
    $this->db->limit($limit, $offset);
    $this->db->join('categories', 'categories.id = posts.cat_id');
    $query = $this->db->get_where('posts', array('cat_id' => $id));
    return $query->result();
}

Upvotes: 1

Related Questions