0

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

1
  • extend the pagination class? Commented Aug 24, 2013 at 13:14

2 Answers 2

2

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

5 Comments

This is not the same from my version, again has to add all this in every function i use pagination? I think it has to have pagination.php but the problem is with initialization global?
@Gorostas First please specify What version of CI you are using? Second, obviously you don't want to initialize pagination library on every Controller, do you?
So it would be as the same as your CI version, I use this approach on v2.1.4 myself. Please let me know what did you exactly do?
@Gorostas, check my updated answer please and take a look at revised old one.
Txanks man, this way in lang works like a charm, solid solution, because pagination.php in config, does not work ok, this is just a turn, but good :)
0

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

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.