gglasses
gglasses

Reputation: 876

How to load external config file for pagination in Codeigniter

I've been on the Codeigniter's documentation and I want to have an external configuration file for pagination, so I did the following:

I put my $config array in config/pagination.php and then on every controller I need to use the configuration file for the pagination I'll use:

$this->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$pag_links = $this->pagination->create_links();

I thought that was all. The problem is that it doesn't work. I searched for this on the site and found these two questions already answered: How to change data in custom loaded config array and autoload config for pagination in codeigniter not working

They both suggested the following line of code:

$this->pagination->initialize($config);

But Codeigniter's documentation says:

Setting preferences in a config file

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called pagination.php, add the $config array in that file. Then save the file in application/config/pagination.php and it will be used automatically. You will NOT need to use $this->pagination->initialize() if you save your preferences in a config file.

Well, The important part is You will NOT need to use $this->pagination->initialize() if you save your preferences in a config file.

Of course, If I put that line of code in my own code it works.

$this->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$pagination->initialize($config);
$pag_links = $this->pagination->create_links();

Am I missing something important here? What's the right way to create an external configuration file for Codeigniter's pagination?

Upvotes: 1

Views: 1398

Answers (1)

Brian Ramsey
Brian Ramsey

Reputation: 850

Your config array should be stored at application/config/pagination.php You then do not need to add:

$this->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;

in the controller only call the library

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

as the config you created have these in there... only thing I can think of if is you are using the variables $total_rows, etc in the config file to be automagically loaded as these would not be referencing anything in an external file... You would need to specify the values directly, e.g.:

$config['total_rows'] = 200;

however, if those values need to be specified inside the controller, you would need to use the way you have said and use the

$pagination->initialize($config);

in the controller.

Upvotes: 1

Related Questions