Miomir Dancevic
Miomir Dancevic

Reputation: 6852

CodeIgniter pagination config without repeating within different Controllers

I have little issue with some messy code, i think it can be better, i use pagination class for displaying the pages, only problem i have is that i want to style it different? Now i have in my controller some functions?

public function products 
{

        $config["per_page"] = 4;
        $config["uri_segment"] = 2;
        $config['cur_tag_open'] = '<li><span class="page active">';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li class="page gradient">';
        $config['num_tag_close'] = '</li>';
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = ' <li class="page gradient">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li class="page gradient">';
        $config['next_tag_close'] = '</li>'
}

And other again the same

public function milks
{

        $config["per_page"] = 4;
        $config["uri_segment"] = 2;
        $config['cur_tag_open'] = '<li><span class="page active">';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li class="page gradient">';
        $config['num_tag_close'] = '</li>';
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = ' <li class="page gradient">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li class="page gradient">';
        $config['next_tag_close'] = '</li>'
}

You see there are the same? But i use it twice and every time i have pagination i must put config in that function, is it possible to style it in global way like

    function __construct()
    {

        $config["per_page"] = 4;
        $config["uri_segment"] = 2;
        $config['cur_tag_open'] = '<li><span class="page active">';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li class="page gradient">';
        $config['num_tag_close'] = '</li>';
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = ' <li class="page gradient">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li class="page gradient">';
        $config['next_tag_close'] = '</li>';
}

That all other pagination that i use in my application use the same styling, and not to repeat it again function by function?

Regards

Upvotes: 0

Views: 1508

Answers (2)

Hashem Qolami
Hashem Qolami

Reputation: 99484

is it possible to style it in global way?

Yes, It is.

Update

From CodeIgniter User Guide:

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: config/pagination.php and it will be used automatically. You will NOT need to use the $this->pagination->initialize function if you save your preferences in a config file.

Old answer

Create a language file (or config file, your choice) and put the pagination config inside:

For example: ./application/language/english/pagination_lang.php:

$lang['pagination_conf'] = array(
    'full_tag_open'   => "<ul>",
    'full_tag_close'  => "</ul>",
    'first_link'      => "First",
    'first_tag_open'  => "<li>",
    'first_tag_close' => "</li>",
    'last_link'       => "Last",
    'last_tag_open'   => "<li>",
    'last_tag_close'  => "</li>"/*,
    And so on... */
);

Then load the language file into your Controller, and pass the value to the config array:

$this->lang->load('pagination');
$config = $this->lang->line('pagination_conf');

In this case, Once you change the source file, everything will be changed.

If you use config file, you can store the values inside an array, and you'll be able to set multiple pagination config:

Config file: ./application/config/pagination_conf.php:

$config['pagination_conf'] = array(
    'case1' => array(
        'full_tag_open'  => '...',
        'full_tag_close' => '...',
        'first_link'     => '...'
        // So on...
    ),
    'case2' => array(
        'full_tag_open'  => '...',
        'full_tag_close' => '...',
        'first_link'     => '...'
        // So on...
    )
);

And you can get access to case1 config by:

$this->config->load('pagination_conf', TRUE);
$config = $this->config->item('case1', 'pagination_conf');

Upvotes: 2

jmadsen
jmadsen

Reputation: 3675

put your configuration file into a file called config/pagination.php & it will be global

Upvotes: 0

Related Questions