CodeIgniter Forums

Full Version: Pagination display issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]E303[/eluser]
Hi all, first of all I would like to say that this is a great frame work to play with.

I am basically making a small blog app to play around with and I am having problems with the pagination side of things.

so far I have a model, which selects all the data in the mysql table in descending order. In my controller however I feel something is wrong because I have set the $config['per_page'] = '1'; yet all 4 posts are displayed on the blog page? The amount of links from the Pagination is correct just not the display..

any help would be greatly appreciated.

El Forum

[eluser]deviant[/eluser]
Don't you still have to take care of using LIMIT in your queries to actually manage the data?

El Forum

[eluser]philm[/eluser]
Yeh, you need to pass 2 values to the model to get the paged results, as opposed to getting ALL results eg.

MODEL
Code:
...
function getPaginated($num, $offset){
        $this->db->orderby("category", "asc");
        $query = $this->db->get('categories', $num, $offset);
        return $query;

    }
...

CONTROLLER
Code:
...
    function viewCategories()
    {
        $data['title'] = "View All Categories";
        $data['heading'] = "View All Categories";

        $this->load->model('Categories');

        // build pagination links
        $this->load->library('pagination');
        $config['base_url'] = site_url("category/viewCategories");
        $config['total_rows'] = $this->db->count_all('categories');
        $config['per_page'] = 10;
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        // get paginatred results
        $num = $config['per_page'];
        $offset = $this->uri->segment(3);
        $this->pagination->initialize($config);
        $data['rows'] = $this->Categories->getPaginated($num, $offset);

        $this->load->view('vw_viewCategories', $data);

    }
...

El Forum

[eluser]E303[/eluser]
thanks for that

El Forum

[eluser]Developer13[/eluser]
Here's how I do it:

The function in the model:
Code:
function getData($page) {
    
    $limit = 10;
    $offset = ($page * $limit) - $limit;

    $this->db->limit($limit, $offset);
    $query = $this->db->get("tablename");
    return ($query->result());

}
This way all you have to do is pass your current page number from the controller. Very simple.