I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
-
why you need to code this in plugin...?? – Pramod Kumar Sharma Aug 23 '12 at 08:08
-
It's just CodeIgniter, so do what you like! Grab information from the URL and feed it into the pagination library as the CodeIgniter docs explain. – Phil Sturgeon Sep 06 '12 at 16:09
2 Answers
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products
function, I am sure you can build the count_all_products()
function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach
in your passed $products
variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.

- 5,444
- 9
- 38
- 50
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

- 7,851
- 5
- 28
- 53
-
thanks for your reply but controller is not involved simply a page and a plugin is all i have to go on with – Muhammad Raheel Aug 23 '12 at 08:53
-
Ok then put this code in plugin.php and use this and can you share your url so i can understand better – Pramod Kumar Sharma Aug 23 '12 at 08:58
-
thanks. i have found a way to do this. created a plugin and two methods for plugin for getting data and creating links then called it in the page. – Muhammad Raheel Aug 27 '12 at 05:39