Part of the EllisLab Network
   
 
CI Pagination Helper
Posted: 28 September 2008 02:33 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  280
Joined  06-11-2007

I must be thick or something as i can’t seem to get the pagination helper to work, mind you, the user guide is very vague on how to make it work too.

This is the function i am trying to paginate (i haven’t started using models yet, so it is a bit long):

function products($categoryid 0$page 10)
    
{        
        $this
->db->select(' product.id AS product_id, 
                            product.name AS product_name, 
                            product.description AS product_description, 
                            product_code, 
                            category.id AS category_id, 
                            category.name AS category_name'
);
        
$this->db->from('product');
        
$this->db->join('category','category.id = product.cat_id','left outer');
        
$this->db->where('product_code != ','');
        
$this->db->where('inactive',0);
        
        if (
$categoryid)
        
{
            $data[
'category_id'$categoryid;
            
$this->db->where('category.id',$categoryid);
        
else {
            $data[
'category_id'0;
        
}
        
        $this
->db->limit($page);
        
        
$this->db->order_by('product_code','asc');

        
$query $this->db->get();
        
        
$this->load->library('pagination');

        
$config['base_url''http://capelite.rayherring.net/admin_catalogue/products/';
        
$config['total_rows'$query->num_rows();
        
$config['per_page''10';
        
$config['uri_segment''4'

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

        
$data['links'$this->pagination->create_links();
        
        
$data['categoryid'$categoryid;
        
$data['result'$query;
        
$this->load->view('header'$this->settings);
        
$this->load->view('shop_bar');
        
$this->load->view('menu');
        
$this->load->view($this->catalogue_folder 'product/list'$data);
        
$this->load->view('footer');
    

and in my view i am doing: <?= $links; ?>

I have almost 5000 records in the database table, and removing the limit causes the browser to hang, so i really need the pagination to work.

Profile
 
 
Posted: 28 September 2008 02:51 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  269
Joined  09-13-2007

I see a potential issue with your code. The base_url you are using are having only 2 segments so it would be 3rd segment having the pagination info. The config you are using for uri_segment is 4 which is incorrect. Can you fix that and check.
Lemme know the outcome and also whats the result.

Have a good day !!!

 Signature 

Sarfaraz Momin.
PHP With Us

Profile
 
 
Posted: 28 September 2008 03:08 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  280
Joined  06-11-2007

ok, so i have moved to a model for it, and done a couple of other things.

however the pagination still isn’t working and i am not getting the links part. The new code is:

Controller Function:

function products($page 0)
    
{        
        $this
->load->model('productmodel');
        
$this->load->library('pagination');

        
$config['base_url''http://capelite.rayherring.net/admin_catalogue/products/';
        
$config['per_page''10';
        
$config['total_rows'$this->productmodel->getProducts($config['per_page'],$page)->num_rows(); 

        
$this->pagination->initialize($config); 
        
        
$data['links'$this->pagination->create_links();
        
        
$data['result'$this->productmodel->getProducts($config['per_page'],$page);
        
$data['categoryid'0;
        
$this->load->view('header'$this->settings);
        
$this->load->view('shop_bar');
        
$this->load->view('menu');
        
$this->load->view($this->catalogue_folder 'product/list'$data);
        
$this->load->view('footer');
    

Model Function:

function getProducts($num$offset$category 0)
    
{
        $this
->db->select(' product.id AS product_id, 
                            product.name AS product_name, 
                            product.description AS product_description, 
                            product_code, 
                            category.id AS category_id, 
                            category.name AS category_name'
);
        
$this->db->from('product');
        
$this->db->join('category','category.id = product.cat_id','left outer');
        
$this->db->where('product_code != ','');
        
$this->db->where('inactive',0);
        
        if (
$category)
        
{
            $this
->db->where('category.id',$category);
        
}
        
        $this
->db->limit($num$offset);
        
$this->db->order_by('product_code','asc');

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

The view hasn’t changed

Profile
 
 
Posted: 28 September 2008 03:11 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  280
Joined  06-11-2007
Sarfaraz Momin - 28 September 2008 06:51 AM

I see a potential issue with your code. The base_url you are using are having only 2 segments so it would be 3rd segment having the pagination info. The config you are using for uri_segment is 4 which is incorrect. Can you fix that and check.
Lemme know the outcome and also whats the result.

Have a good day !!!

The uri segment was fine in the first listing, ‘admin_catalogue’ was the controller and ‘products’ was the function, so segment 3 would have been ‘category’ (assuming it was passed) which means that ‘page’ would have been segment 4.

Profile
 
 
Posted: 28 September 2008 04:47 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  280
Joined  06-11-2007

ok, so i worked out what the problem was, the problem was that the ‘num_rows’ was being set to $query->num_rows(), but since the query was being limited the pagination class was only ever going to see 1 page of results and never anymore.

i created a new function in my model to count all the results that match the query (minus the limiter) and now the page links all come up properly.

Profile