1

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?

2 Answers 2

6

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,
      // ...
 )));
Sign up to request clarification or add additional context in comments.

5 Comments

So this means that I will not use the Pagination.php config file, which is apparently used by the Pagination class automatically?
Yes, if you copy the example then you won't. But since the initialize() only copies over the keys you provide to the defaults, it should work without explicitly merge in the $this->config->item('pager') array.
I've redid the answer, making a separate config array proved to be superfluous.
Seems to work indeed, so thanks! Having some of the pagination parameters in the config file, I need to read them to pass into the querymethod in the model: $this->load->config('pagination'); $per_page = $this->config->item('per_page'); (this value is passed as the limit parameter) Seems to work OK but it is not fully clear how to use $this->config, does it always refer to the last loaded configuration file?
This is clear now too after reading the documentation once more :) Loading a config file just add the configdata to the config array, in case of possible name conflits a second parameter can be used.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.