Peanut
Peanut

Reputation: 3295

CodeIgniter Pagination not showing all rows that should be shown

Ok, I have been at this for the past hour and CAN NOT for the life of me figure this out. It's saying LIMIT 2, 25.. Which is of course skipping over two of the rows that should be displaying... I'm still on page 1, the links don't even show either! The first page, I would think needs to be 0, 25 because that shows all the rows that should be shown on the first page.

Now I only have 5 rows thus far, but it's only showing 3 of them... The first two rows are NOT being shown that the query originally generates until I add the LIMIT clause. Here is what I have programming wise:

    public function category($id, $page = NULL) {
    //grab topics
    parent::before();
    $data['forum_title'] = $this->forum->get_cat($id);
    $data['id'] = $id;

    $config = array();
    $config['base_url'] = base_url() . 'forum/category/';
    $config['total_rows'] = $this->forum->topic_count($id);
    $config['per_page'] = 25;
    $config['uri_segment'] = 4;

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['topics_array'] = $this->forum->get_topics($id, $config['per_page'], $page);

    $data['links'] = $this->pagination->create_links();
    $this->load->view('forum/category', $data);
    parent::after();
}

I will admit I do NOT understand the URI_SEGMENT ordeal.. I kind of do, but not really. I was following a tutorial to generate this. But I print my $limit and $start variables and the limit is 25 like it should be, but the start prints as 2.. when I would think it should be 0 on the first page. There shouldn't even BE a second page yet since there aren't 25 results yet.

Here is my functions to generate those. The count_all_results() function works (brings back a total of 5 results). But the get_topics() function is NOT working properly whenever I add the LIMIT clause.

//count the topics in a certain category id for pagination
public function topic_count($id) {
    $this->db->where('cat_id', $id);
    $this->db->from('forum_topic');

    return $this->db->count_all_results();

}
//get all topics in a certain category
public function get_topics($id, $limit, $start) {
    $this->db->distinct();
    $this->db->select('t.id, t.title, t.sticky, t.locked, u.username as author, COUNT(p.id) as postcount, (SELECT u.username
        FROM forum_post fp
        INNER JOIN user u ON fp.author_id = u.user_id
        WHERE fp.topic_id = t.id
        ORDER BY fp.date DESC
        LIMIT 1) as lposter, (SELECT fp.date
        FROM forum_post fp
        WHERE fp.topic_id = t.id
        ORDER BY fp.date DESC
        LIMIT 1) as lastdate');
    $this->db->from('forum_topic t');
    $this->db->join('user u', 'u.user_id = t.author_id', 'inner');
    $this->db->join('forum_post p', 'p.topic_id = t.id', 'inner');
    $this->db->where('t.cat_id', $id);
    $this->db->group_by('t.id, t.title, t.sticky, t.locked, author, lposter, lastdate');
    $this->db->order_by('t.sticky desc, p.date desc');
    $this->db->limit($limit, $start);
    return $this->db->get()->result();
}

My URL I am at right now is forum/category/2... So I'm assuming the pages will go like this: forum/category/2/1 for page 1, forum/category/2/2 for page 2, etc. Correct?? So I put 4 as the URI segment since it's in the 4th argument position.

Any help would be much appreciated...

EDITTTTTTTTT

Ok someone has helped me with this. However, when I click on Page 2 (I lowered the limit to 5 for right now and currently have 7 results so it generated a page 2... It's replacing the ID 2 (Category ID of 2) with 5.. instead of going to page 2??.... Page 2 should look like this : forum/category/2/2... But instead it's doing this : forum/category/5

Upvotes: 0

Views: 1178

Answers (1)

ithcy
ithcy

Reputation: 5589

You're using 2 different URI segments. You're initializing the pagination object with 4, and then you're sending segment 3 to get_topics() (because $this->uri->segment(3)). You should probably be using 3 for both, since URI segments start at 0 not 1.

Either way, whether you use 3 or 4, you should use the same one in both places. Use $this->uri->segment($config['uri_segment']) instead of $this->uri->segment(3).

Upvotes: 2

Related Questions