• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI Pagination Helper

#1
[eluser]ray73864[/eluser]
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):

Code:
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.

#2
[eluser]Sarfaraz Momin[/eluser]
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 !!!

#3
[eluser]ray73864[/eluser]
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:
Code:
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:
Code:
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

#4
[eluser]ray73864[/eluser]
[quote author="Sarfaraz Momin" date="1222602712"]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 !!![/quote]

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.

#5
[eluser]ray73864[/eluser]
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.