Part of the EllisLab Network
   
 
CI Pagination Helper
Posted: 28 September 2008 01:33 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  282
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 01:51 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  260
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 02:08 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  282
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 02:11 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  282
Joined  06-11-2007
Sarfaraz Momin - 28 September 2008 01: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 03:47 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  282
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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120268 Total Logged-in Users: 34
Total Topics: 126397 Total Anonymous Users: 5
Total Replies: 664725 Total Guests: 330
Total Posts: 791122    
Members ( View Memberlist )