CodeIgniter: howto override Pagination configuration in controller code? - codeigniter

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?

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,
// ...
)));

don't use base_url()... I always use site_url();
$config['base_url'] = site_url('');

Related

how to make codeigniter routing to work with paging

I have the following routes for the news that works like a charm:
$word5 = urlencode('mygreek-keyword-here');
$route[$word5] = "news/displayAllNews";
and this page display all news with the desired keyword in the address bar. But...there is always a but...I have paging on this page, so when i click on next page link...page works but with old non-friendly seo url, that is:
news/displayAllNews/10/10
while I would like to have is: mygreek-keyword-here/10/10
code that i have in my controller for the paging is as follow:
$config['base_url'] = site_url('/news/displayAllNews/'.$limit.'/');
what should i do in routes.php and in my controller to get the desired result?
Regards, John
You could get the string of URI by $this->uri->uri_string() method, and set the pagination config as follows:
$config['base_url'] = site_url($this->uri->uri_string() .'/'. $limit.'/');
Note that the URI library is loaded automatically, it doesn't need to load it manually.
Update:
Change the route rule to:
$route["$word5(.*)"] = "news/displayAllNews$1";
If it doesn't work yet, remove the .'/'. $limit.'/' phrase and let the pagination to add the limit itself by:
$config['per_page'] = 10; // or $limit, default items per page.
$config['use_page_numbers'] = FALSE;

CodeIgniter Pagination configuration is wrong?

Please take a look at the following code in a controller named "test":
$this->load->library('pagination');
// These 3 are the minimum config that needs to be set
$config['base_url'] = base_url() . 'test/?' . http_build_query($_GET, '', '&');
$config['total_rows'] = 173;
$config['per_page'] = 20;
// This returns something like this: ?name=foo/20
// I would expect something like this: ?name=foo&per_page=20
// $config['enable_query_strings'] = TRUE;
// This retuns something like this: ?name=foo&per_page=20 (which is what I want),
// but the documentation says it should be: ?c=test&m=index&per_page=20
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
Then go to: test/?name=foo
The resulting pagination links look correct. When you click on, say, link number "2", you are redirected to ?name=foo&per_page=20 which is correct. However, the new pagination created looks wrong. It looks something like this: ?name=foo&per_page=20&per_page=40 (where per_page appears twice in the query string).
What's going on?
add uri segments
$config['uri_segment'] = x ;

Pagination on Pyrocms pages

I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

Codeigniter's Pagination is not working

I am trying to display information from mysql database based on user's query . I have a form and after a user submits data, it goes to the controller I have posted below and then to a view file where it shows the information the user wanted.
So far I am good with this, but the problem creates with the pagination. At present I have eight rows of data to display from my database, so to check if the pagination is working well I set the $config['per_page'] value to 5 (please check below). And now when I click on the next button, it doesn't show any data.
I think the problem lies in the fact that when I click on the "next" button, it goes to the controller again but this time the controller is not getting the values the user posted in the first place.
Would you please kindly help me with this?
Thank in Advance :)
My Controller:
function show(){
$batchid=$this->input->post('batchid');
$daterange1=$this->input->post('daterange1');
$daterange2=$this->input->post('daterange2');
$this->load->library('pagination');
$config['base_url'] = base_url().'markslist/show';
$this->db->select('*');
$this->db->from('marks');
$this->db->where('examdate', $daterange1);
$this->db->where('examdate', $daterange2);
$this->db->join('student', 'marks.studentid = student.studentid');
$this->db->where('batchid', $batchid);
$this->db->order_by('batchid','DESC');
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 5;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
// To get the subject lists
$this->load->model('mod_markslist');
$data['records']= $this->mod_markslist->listmarks($batchid,$daterange1,$daterange2,$config['per_page'],$this->uri->segment(3));
$data['main_content']='view_markslist';
$this->load->view('includes/template',$data);
}
I would copy the initial posted data to session:
if($this->input->post()){
$this->session->set_userdata('batchid', $this->input->post('batchid'));
$batchid = $this->input->post('batchid');
/* similar for the other posted fields */
}
else{
$batchid = $this->session->userdata('batchid');
/* similar for other fields now in session */
}
/* load pagination library etc */
Best to also do a check if the information is available in session as well though.

Autoload Config for Pagination in CodeIgniter not working

I am trying to implement pagination in my CI webapp. Now I put the config for pagination inside a config file like this...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = "http://example.com/index.php/home/index";
$config['num_links'] = "9";
$config['per_page'] = "20";
$config['total_rows'] = "200";
/* End of file pagination.php */
/* Location: ./system/application/config/pagination.php */
In my controller, I have loaded the library
$this->load->library("pagination");
And I have defined the pagination config file to be autoload in config/autoload.php
$autoload['config'] = array('pagination');
At last I called the method to create links in my view template:
<?php echo $this->pagination->create_links(); ?>
This did not create any links. The configuration is being autoloaded correctly. I checked using...
<?php echo $this->config->item("num_links"); ?> <!-- this dislayed 9 -->
What am I missing here? Just for the record, putting the config inside the controller didn't work either.
Update #1- I have found out that the config settings are loading correctly but they are not reaching the library or something like that. Inside the pagination library - I did some hard coding to find out that per_page parameter was zero in there.
Update #2- I was mistaken when I said that putting the config inline wasn't working. It is working fine. The autoload isn't working.
Regards
Finally used this code to solve my problem...
$this->config->load("pagination");
$page_limit = $this->config->item("per_page");
$config['total_rows'] = $var; // Some variable count
$this->pagination->initialize($config);
This lets me define the config items in a file as well as initialize the items I want in controller like in my case, the total no. of rows - retrieved from database.
Regards
Your autoload line in your config file should be this
$autoload['libraries'] = array('pagination');
And you must have this line in you controller after your config array, before you use create_links() etc.
$this->pagination->initialize($config);

Resources