• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Storing pagination settings and changing them dynamically

#1
[eluser]yogal[/eluser]
Solution:

If you want to change pagination settings dynamically based on user's choice - use database. Then retrieve the settings and initialize pagination class with
Code:
$this->pagination->initialize($db_config);

I hope this helps.




Hello,

I'm building a small CMS in CI and I'm using Ci's default pagination library (CI ver. 1.7.0). I would like to store configuration settings in pagination.php file and let users change them if they want.

This is my pagination.php file
Code:
$config['per_page'] = '10';
    $config['page_query_string'] = FALSE;
    $config['full_tag_open'] = '';
    $config['full_tag_close'] = '';
    //First link
    $config['first_link'] = '‹ Unol';
    $config['first_tag_open'] = '';
    $config['first_tag_close'] = ' ';
    //Last link
    $config['last_link'] = 'Last ›';
    $config['last_tag_open'] = ' ';
    $config['last_tag_close'] = '';
    //Next link
    $config['next_link'] = '>';
    $config['next_tag_open'] = ' ';
    $config['next_tag_close'] = ' ';
    //Previous link
    $config['prev_link'] = '<';
    $config['prev_tag_open'] = ' ';
    $config['prev_tag_close'] = '';
    //Current page
    $config['cur_tag_open'] = '&nbsp;<b>';
    $config['cur_tag_close'] = '</b>';
    //Digit link
    $config['num_tag_open'] = '&nbsp;';
    $config['num_tag_close'] = '';

With every page there are few things that change:
- base_url
- total_rows

What i would like to do is set these config items in each controller separately and for the rest - use the default values from pagination.php

Quote: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: 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.

Knowing that, I am setting the pagination config with
Code:
$this->config->set_item('total_rows', $smth);
etc.

Here's part of my controller:

Code:
$this->load->model('Guestbook_model', 'Guestbook_model');
            $this->load->library('pagination');

            $this->config->set_item('per_page', 10);
            $total_rows = $this->Guestbook_model->count_entries();
            $this->config->set_item('total_rows', $total_rows);
            $this->load->library('pagination');

Now, this doesn't work. It seems to ignore the settings I provide here, total_rows are set to 0 - and therefore no pagination links are created.
It doesn't matter if $config['total_rows'] (in pagination.php) is already set to '' , 0, NULL or not specified at all - as long as it is not a number (> 0) it's assumed 0 despite setting it through controller.
I have to manually set this in config file - which works but doesn't allow me to change it dynamically:

Code:
$config['total_rows'] = '34';

However, the same command works with setting $config['per_page'] :

Code:
$this->config->set_item('per_page', 30);

The case with base_url is diffrient. When set through $this->config->set_item:
Code:
$this->config->set_item('base_url', base_url().'/admin/guestbook/');
it's interfering with $config['base_url'] in main config.php file used by CI. The workaround would be to replace pagination library and prefix base_url with something (use a diffrient index).

I can set all the settings without problems when reaching variables in pagination library directly:
Code:
$this->pagination->base_url = base_url().'/admin/guestbook/';
But I know it's a very bad thing to do.

I've seen a few similar topics about this, some ppl claimed that changing pagination.php to Pagination.php had helped although for me, it doesn't matter - I can set all the values in config file and they are used/working, the problem is with changing the $config['total_rows'] dynamically.

Please, tell me if it's possible to change these values dynamically or what am I doing wrong.

Any help will be apriciated.

Regards,
yogal

#2
[eluser]Colin Williams[/eluser]
The pagination class has an initialize function. I would store my config options in a DB, then load them up with this function. $this->pagination->initialize($config);

#3
[eluser]yogal[/eluser]
Hi,

thank you for such a fast reply.
I must admit it's a brilliant idea. Having read about the configs and all I overlooked such a simple approach.

But then, what is the pagination.php config file for? Is it really making things easier ?

I think i will stick with a method proposed by you - it's bound to work this way. Still, part of me cant get over the "problem". I would appreciate if someone could clear things up a bit, maybe it's a CI bug ?

Thanks,
yogal

#4
[eluser]Colin Williams[/eluser]
Quote:But then, what is the pagination.php config file for? Is it really making things easier ?

Things have been made flexible. The pagination.php config file is good for defaults. Then, the CI team made an initialization method to act as sort of a re-constructor.

You typically can't just change a library's settings through the config methods. These settings get applied when the given library class is instantiated. So, changing them thereafter has no effect.

#5
[eluser]yogal[/eluser]
Yea I guess a config file is usefull when you have lots of pages with pagination and just some default setting. You can then change them manually and everything will be updated.
I will mark this topic as read.

Thank again.

#6
[eluser]J. Pavel Espinal[/eluser]
A good example of using pagination.php for default settings, is when you have a funny designer who made the pagination as part of the design Big Grin, then you have to use a lot of '<div>' and stuff in order to make some things as default, e.g:

$config['prev_tag_open'] = '<div class="botton_pagin">';
$config['previous_link'] = '<img src="'. base_url() .'images/arrow_left.png" alt="Anterior" width="36" height="36" border="0" />';
$config['prev_tag_open'] = '</div>';

Under these circumstances, you would not want to do that manually over and over and over again (Note: that's just a little bit of how the entire 'default' should look like in my case).

If 'pagination.php' wouldn't exist, even Freddy Krueger would not be able to plan a worst nightmare Tongue


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.