How to fix codeigniter pagination and route - codeigniter

I have problem whit codeignitet pagination and route!
I set the route my function class like this:
$route['admin/panel/students/new-stuedents-list/:num'] = "admin/newStuedentsList/$1";
then in my controller, I create a function that call newStuedentsList and load pagination library, every things work fine but pagination nav... :(
the page loaded successfully...
and the data is correct...
bat when I click for example page 2 , the page 2 loaded successfully but the pagination nav show page i button ...!
when I call page 4 form url (http:// localhost/d/index.php/admin/panel/students/new-stuedents-list/30) , again the data is correctly ... but the pagination nav show page 1 button and the number of page don't change!
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/panel/students/new-stuedents-list/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(5));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
but when the newStuedentsList is:
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/newStuedentsList/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(3));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
every things work fine!
How do I fix this problem ...?

You forgot one configuration:
$config['uri_segment'] = 5;
To determines which segment of your URI contains the page number.
And replace pre_page by per_page

Related

Page number in codeigniter

I am new user of codeigniter.
I want to show my output of my data from database as on different pages.
If total 200 entries 50 in one page and then next and so on .
Please help.
Any suggestion?
Codeigniter supports pagination by default.
/* Controller */
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 50;
$this->pagination->initialize($config);
/* View */
// Print it in the view file
echo $this->pagination->create_links();
More info: ellislab documentation

Pagination past limit in codeigniter

The code listing below is in a function in a message controller. I want that a page to only have 5 records at a time on a page. Using pagination class in codeignuter i have set the limits meaning that if a page has only three records then in the pagination link in the view only one is shown e.g < 1 and not < 1 2. This also happens with the last page, if the pages are 8 it shows 9 thus <... 8 9. What am i missing
$data['message_available'] = $this->getCheckForMessages();
$config["base_url"] = base_url() . "messages/inbox/";
$config['total_rows'] = $this->getMessageCount();
$config['per_page'] = 5;
$config["num_links"] = 10;
$config["full_tag_open"] = '<div id="pagination">';
$config["full_tag_close"] = '</div>';
$this->pagination->initialize($config);
$data['messages'] = $this->getMessages($config['per_page'], $this->uri->segment(3));
$data['links'] = $this->pagination->create_links();
You have to check deeply this :
$config['total_rows'] = $this->getMessageCount();
maybe is returning some field more than the Results query getMessages();
please post also your getMessages() and getMessageCount(); queries to be more accurated
also please change this:
$config["base_url"] = base_url() . "messages/inbox/";
to this:
$config["base_url"] = site_url('messages/inbox');

Codeigniter Pagination - Limit and Limit Offset query strings

I have config code for Codeigniter's pagination
$config['base_url'] = $base_url;
$config['total_rows'] = $total_search_results;
$config['per_page'] = $per_page;
$config['num_links'] = 4;
$config['use_page_numbers'] = FALSE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'limit-offset';
I have "limit" and "limit-offset" values that are gotten from GET query strings which is where I derive the $per_page value.
However, in the pagination links that are produced, I still want to include the "limit" and "limit-offset" values in a url like
www.domain.com/test/?limit=10&limit-offset=20
How do we do these using Codeigniter Pagination library?
Refer to #WesleyMurch's answer here at Pagnation with GET data in the uri - Codeigniter
// After loading the pagination class
$this->pagination->suffix = '{YOUR QUERY STRING}';
Or better yet, just add $config['suffix'] = '{YOUR QUERY STRING}'; to your config before loading the class.
You must edit your
config["base_url"] = www.domain.com/test?limit=xxx;
edit
config['per_page'] = $this->input->get("limit");
This is the complete config :
//your base url : www.domain.com/
$config['base_url'] = sprintf("%stest/?limit=%d", $base_url, $this->input->get("limit")); // it will generate : www.domain.com/test?limit=xxx
$config['total_rows'] = $total_search_results;
$config['per_page'] = $this->input->get("limit");
$config['num_links'] = 4;
$config['use_page_numbers'] = FALSE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'limit-offset';
Then, foreach pagination link will have this format :
www.domain.com/test/?limit=10&limit-offset=20

Pagination in codeigniter

Help me my url in pagination ! :(
$data['total'] = $this->news_model->all_list();
$this->load->library('pagination');
$config['base_url'] = site_url().'admin/news/page';
$config['total_rows'] = count($data['total']);
$config['per_page'] = '5';
$this->pagination->initialize($config);
$data['columns'] = array('Tile', 'Date', 'User', 'Active', 'Edit', 'Delete');
$data['list'] = $this->tintuc_model->all_list($config['per_page'],$this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->smarty->view( 'admin/news.tpl', $data );
That's my code. List of pages showed ok. But, when all links are wrong.
eg: when i click the 2 page link => http://localhost/mysite/admin/news/page/5 ?
Why it is 5 instead of 2 ?
you can change
$config['per_page'] = '5';
to
$config['per page'] = '2';
in order to get 2 in your uri segment, but it indicates the limit of data to be displayed in your page not the clicked pagination number.
It's not the page number - it's page number * items per page
In your case $config['per_page'] = '5'; So everything seems to be okay.
You need to set:
$config['use_page_numbers'] = TRUE;
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

CodeIgniter routes and pagination adding “/page/” to all links

I’ve implemented pagination like the following:
$this->load->library('pagination');
$perpage=10;
$config['base_url'] = site_url().'news/page';
$config['total_rows'] = $this->news_model->getnews(array('count' => true));
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$config['num_links'] = 8;
$news = $this->news_model->getnews(array('limit' => $perpage,'offset'=>$offset));
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['news'] = $news;
$data['page'] = "news";
$this->load->view('index', $data);
I’m also using the following routes:
$route["news"] = "news/news_list";
$route["news/page"] = "news/news_list";
$route["news/page/(:num)"] = "news/news_list/$1";
$route["news/detail/(:any)"] = "news/news_detail/$1";
The problem that I’m facing is that although the pagination is working fine when i go to the second page or any other page after clicking on the pagination links - all of my other links on the page get the /page/ added in front of them like -> /page/detail/aaaaaa so that my route $route["news/detail/(:any)"] = "news/news_detail/$1"; can not identify it as the detail link.
Why is the /page/ added to all of the links? Do i need any routes for Pagination?
Your $config['base_url'] is news/page, that’s why /page is added to all your links.
I don’t think you need these routes for pagination, but if you want them, you should use these routes in $config['base_url'].
$route["news/page/(:num)"] = "news/news_list/$2";
$route["news/detail/(:any)"] = "news/news_detail/$1";

Resources