CodeIgniter Forums
Very stuck - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Very stuck (/showthread.php?tid=27092)



Very stuck - El Forum - 02-01-2010

[eluser]Bainzy[/eluser]
Hello,

I am currently very stuck with implimenting the Pagination library with my code.

I am developing a forum and at present i have everything working correctly, however i can no think of a way of adding pagination into my scripts.

What i have is the main forum listing, here you can see all the topics and various topic information, part of this information is the category that the topic is posted in, now if the user clicks this category the page is then sorted to only display topics in that category, here is a example of my "topics" controller :

Code:
function topics()
    {
        $data['topics'] = $this->MTopics->get_listings($this->uri->segment(3));
        $data['category'] = $this->MCats->get_current_cat($this->uri->segment(3));
        $this->load->view('forums', $data);
    }

Here is the MTopics Model sample code :

Code:
function get_listings($category)
    {
        $data = array();
        
    if ($category)
    {
       $this->db->select('*');
       $this->db->join('category', 'category.CategoryID = topics.CategoryID');
       $options = array('topics.CategoryID'=>$category);
       $this->db->order_by('TopicID', 'desc');
       $q = $this->db->get_where('topics', $options);
      
    }
    else
    {
       $this->db->select('*');
       $this->db->join('category', 'category.CategoryID = topics.CategoryID');
       $this->db->order_by('TopicID', 'desc');
       $q = $this->db->get('topics');
       $rowcount = $q->num_rows();
    }        
        if($q->num_rows() >0)
        {
            foreach ($q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        
        $q->free_result();
        return $data;
    }

and my view file is a simple foreach statement.

How can i go around implementing pagination on this ?

Regards
Chris


Very stuck - El Forum - 02-02-2010

[eluser]jedd[/eluser]
Hi Chris,

[quote author="Bainzy" date="1265042847"]
I am currently very stuck with implimenting the Pagination library with my code.
[/quote]

Is this a follow on from [url="/forums/viewthread/143916/"]this thread[/url]?


Quote:I am developing a forum and at present i have everything working correctly, however i can no think of a way of adding pagination into my scripts.

I'm not clear on what you're actually getting stuck with. Pagination's not hugely complex, once you look at a few of the tutorials and example code partials.

The principle is pretty straightforward - you get a count of the results first (including your 'WHERE' qualifier that you mentioned in the previous thread) and then you do your pagination call (perhaps not calling it if your things-per-page setting is less than the COUNT you got from that query) back to the model, passing the LIMIT and offset for the SQL call.