klausch
klausch

Reputation: 662

CodeIgniter: howto override Pagination configuration in controller code?

I recently jumped on the CodeIgniter train because I finally want to embrace the MVC architecture for my growing PHP/MySQL projects. Right now I encounter a probolemin the PAgination configuratino that is apparently not described earlier as far as I can ssee.

Probably the issue is not PAgination only related but more general. In my first attempt, I just filled the confiuration in the controller method and passed it to the initialize() method:

$config['base_url'] = $this->config->base_url() .  "/categorie/{$catName}/";
$config['total_rows'] =      $this->event_model->get_nrofevents_for_categorie($categorieObject->id, TRUE);
$config['per_page'] = 12;
$config['full_tag_open'] = '<div class="paging">';
$config['full_Tag_close'] = '</div>';
$config['prev_link'] = 'Vorige';
$config['next_link'] = 'Volgende';
$this->pagination->initialize($config);

And this works fine. But I have a buch of other pages that use the same pagination with most of its parameteres identical, only the base_url and the total_rows config properties are different for each page. I have put the common configuration in the config/pagination.php configuration file but see no option to add the page-dependent properties in the code. More general, is it possible to put generic configuration in a config file and add some customized properties in the controller method? For me this seems logical but cannot figure out how... I tried something like:

$this->load->config('pagination');
$this->config->set_item('next_link', 'blablabla');

but it seems that the Pagination is initialized immediately after reading the config file and this code has no effect. Please any suggestions?

Upvotes: 1

Views: 2760

Answers (2)

noushid p
noushid p

Reputation: 1483

don't use base_url()... I always use site_url();

$config['base_url'] = site_url('');

Upvotes: 0

complex857
complex857

Reputation: 20753

Since the initialize() only replaces the keys you provide, you can have your config/pagination.php hold the default values and call initialize() with the changing ones.

config/pagination.php

 // put default values here
 $config['next_link'] = 'Volgende';
 $config['prev_link'] = 'Vorige';
 // ...

controller

 $this->pagination->initialize(array(
     'base_url' => base_url().'categorie/'.$catName.'/',
     'total_rows' => $totalRows,
      // ...
 )));

Upvotes: 6

Related Questions