0

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?

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103

2 Answers2

1

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.

Alireza
  • 5,444
  • 9
  • 38
  • 50
0

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();
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53