Member Avatar for iamthwee

So there is a lot of useful stuff that comes with code igniter. I'm looking at the pagination class and whilst it works well with table dumps I'm thinking I'd have to seriously hack it to get anything remotely useful or relevant to what I need.

So was just thinking about writing my own pagination class specific to me? What are your thoughts for those who have used/use code igniter?

cheers.

Recommended Answers

All 3 Replies

that could work pretty well also. You can put your pagination class in the application/libraries and then include it in the application/config/autoload.php

$autoload['libraries'] = array('database', 'session', 'YourpaginationClassName');

Alternatively we can also load it as needed by either loading it on the controller's constructor or (in model's constructor if necessary).

public function __construct(){

    $this->load->library('YourpaginationClassName');

    }

It can get a little bit tricky as we try to get the current page, so we have to load the url helper function by placing it just above the pagination class. Actually it does not matter, but pagination class will later become dependent on the routed page number e.g. /2 or /++i

  public function __construct(){

    $this->load->helper(array('url'));
    $this->load->library('YourpaginationClassName');


    }

on our index method with page number from the url helper

 $current_page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

 ## instantiate your pagination class
 $paginate_it = pagination($current_page,$count);

CodeIgniter's built-in pagination class can also be use in parsed data from rss feeds, xml files. As long as we can get the total count it can work on CI. Below is a classic CI pagination calls

$count = 'Count of Items to be paginated';
$config = array();
    $config["base_url"] = base_url() . "/yourController/";
    $config["total_rows"] = $count;
    $config["per_page"] = 20;
    $config["uri_segment"] = 2;
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);

    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

    ## this can be use on the view page or template page
    $paginate_it = $this->pagination->create_links() 

Lastly, we can adjust our router page to the controllers name of the page where the pagination should take place. application/config/routes.php

$route['NameOfYourController/:num'] = 'ControllerClassName';

I love CI because it was my training ground. Unlike other frameworks, CI allows us to load libraries as needed and not a flash-flood loading kind of thing.

Sorry :), this

 ## this can be use on the view page or template page
$paginate_it = $this->pagination->create_links() 

should read

 ## this can be use on the view page or template page
$paginate_it = $this->pagination->create_links(); 
Member Avatar for iamthwee

Thanks for your reply, I will need some time to digest that although I'm still unsure as the data it will be pulling back will be quite complicated... Hmmm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.