CodeIgniter Forums
Pagination - Am I Missing something about per_page? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Pagination - Am I Missing something about per_page? (/thread-25949.html)



Pagination - Am I Missing something about per_page? - El Forum - 01-01-2010

[eluser]developer10[/eluser]
I'm wondering what's the purpose of $config['per_page'] item if it doesnt automatically
splits my records in order to show them in a defined number per page?

i set it, let's say, 10 items per page, and still pagination loads them all. then i need to manually insert
LIMIT in my query, but then again, i have to mess with other things again.

my model function:

Code:
function getUserInputs($username)
    {    
        $config['per_page'] = 12;

        $this->db->select()->from('_tbl_firme');
        $this->db->join('users', '_tbl_firme.id = users.id', 'left');
        $this->db->where('username', $username);
        $this->db->order_by('f_vrijeme', 'desc');
//        $this->db->limit($config['per_page'],0);
        $query = $this->db->get();
        
        $pagBase = site_url().uri_string().'/start';
        $config['base_url'] = $pagBase;
        $config['num_links'] = 4;
        $config['uri_segment'] = $this->uri->total_segments();

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

        if($query->num_rows() > 0) {
            $result = $query->result();
        }
        
        return $result;
    }

maybe someone would be kind enough to explain to me how to use this item properly.
thanks!


Pagination - Am I Missing something about per_page? - El Forum - 01-01-2010

[eluser]Twisted1919[/eluser]
Why initialize the pagination in the model ? This isn't a good practice at all(my opinion)
if i where you , in my model i would just execute the query with the limit and the offset , then i would generate the pagination in my controller .
For instance :
Code:
$items = $this->model->count_items();
$per_page = 10 ;
$segment = (int)$this->uri->total_segments() ;

if( $items <= $per_page )
{
$data['pagination'] = 'Page 1 of 1';
$data['items'] = $this->model->get_items(0,$per_page);
}
else
{
$this->load->library('pagination');
//Pagination config here //
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['items'] = $this->model->get_items($segment,$per_page);
}



Pagination - Am I Missing something about per_page? - El Forum - 01-01-2010

[eluser]Aken[/eluser]
The Pagination class's function is to create the links for the different pages. It has no other function than that. It is up to you to discern what information need to be displayed based on the URL.

The class should be used in your controller, and the create_links() function should be passed through to your view. Your controller should then parse the URL to figure out what chunk of information should be retrieved, and then passed to your model, which is where you'd include the LIMIT query.


Pagination - Am I Missing something about per_page? - El Forum - 01-02-2010

[eluser]Colin Williams[/eluser]
The user guide could be more helpful here. It should have a red notice at the top, reading something like "The pagination class handles all the logic for displaying pagination links. It does not automatically LIMIT or OFFSET your database queries."


Pagination - Am I Missing something about per_page? - El Forum - 01-02-2010

[eluser]developer10[/eluser]
even though i have prevously successfully implemented pagination in CI, i didn't realize until now that its solely purpose is that what you're saying about - only displaying links.

i was tricked by that config item per_page. i thought it existed in order for the class to know how many records to show per page. i know now that it is needed because pagination class
must calculate how many (numeric) links it will generate below the records.

i might just ask one more question: how do you handle $base_url for pagination when there might occur different number of uri segments?

i went with associative uris, putting some (IF)logic, which checked if there were possible uri segments and appending them to the end of $base accordingly. it worked well, and if there's
no better solution, i'll just stick to it.