Reputation: 2749
According to the CI official documentation it's possible to set preferences in a config file, but I can't get this to work.
I think I have narrowed the issue down to line 3 of my pagination.php file - the error message I receive is;
Message: Undefined property: CI_Loader::$db
Message: Call to a member function get() on null
Whenever I change this line of code from;
$config['total_rows'] = $this->db->get('item')->num_rows();
to
$config['total_rows'] = 200;
Everything works as expected. Am I failing to load something or call the database?
My code is:
Controller
public function index() {
$this->load->library('pagination');
$data = array(
'items' => $this->items_model->itemList(),
'title' => 'Library Items'
);
$this->load->view('item_list', $data);
}
Model
public function itemList() {
$this->db->select('item_id, item_title');
$this->db->from('item');
$this->db->limit(5, $this->uri->segment(3));
return $this->db->get()->result();
}
View
<?php echo $this->pagination->create_links() ?>
pagination.php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['base_url'] = '/ci/items/index';
$config['total_rows'] = $this->db->get('item')->num_rows();// when I change this to `$config['total_rows'] = 200` it works
$config['per_page'] = 5;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div><ul class="pagination">';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$config['anchor_class'] = 'follow_link';
Thanks
Upvotes: 0
Views: 4580
Reputation: 1171
If you keep changing the question it makes previous answers look a bit strange. However you cannot load the db or use a db call in your config file. If you have to run a database query to get a value you are no longer using a config setting, but assigning a value to an arguement for use in a library method call.
So just set the config value to some fixed reasonable value, say 50, then in your controller override the setting using something like:
// load config file
$this->config->load('pagination', TRUE);
// access pagination settings
$settings = $this->config->item('pagination');
// change whatever you need to
$settings['total_rows'] = $this->db->get('item')->num_rows();
// use the settings to initialize the library
$this->pagination->initialize($settings);
There might be an easier way but as far as I know there are no other setters in the pagination library.
Upvotes: 1
Reputation: 868
you can make a config file with name pagination
in config folder, its called automatically when you load library.
in controller add custom config and finally $this->pagination->initialize($data);
final code is something like this:
$this->load->library('pagination');
$config['base_url'] = base_url('task/lists/');
$config['total_rows'] = $this->tasks->lists_user_count();
$config['per_page'] = 30;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
other config like $config['num_links'] = 2;
is in config file.
Upvotes: 0
Reputation: 1171
In your code you have written the line:
$this->pagination->initialize($data);
This line is not needed if your pagination is in a config file.
To set your preferences in a config file just create a file in the config folder called 'pagination.php'. Your file would be something like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['base_url'] = '/ci/items/index';
$config['per_page'] = 5;
$config['num_links'] = 5;
....etc etc
When you load the pagination class these will all be automatically loaded without you having to use the initialize function as mentioned above. So your controller will now look something like this:
public function index() {
$data['total_rows'] = $this->db->get('item')->num_rows();
//start pagination
$this->load->library('pagination');
$data = array(
'items' => $this->items_model->itemList(),
'title' => 'Items'
);
$this->load->view('item_list', $data);
}
Hope that helps.
Upvotes: 0