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

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";

Related

How to fix codeigniter pagination and route

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

Codeigniter Pagination having page number in the middle of url

I'm trying to use pagination class in codeigniter and my url looks something like this:
blah.com/posts/browse/page/1/item_per_page/10
is there anyway to keep the page number in the middle of url?
Thanks
EDIT:
$this->load->library('pagination');
$uri = $this->uri->uri_to_assoc();
$page = null;
$item_per_page = null;
if (count($uri))
{
foreach ($uri as $key => $value)
{
$$key = $value;
}
}
$config['base_url'] = base_url('posts/browse/page//item_per_page/1');
$config['uri_segment'] = 4;
$config['per_page'] = '1';
After digging through code of Pagination class, I found a way to do this, but it wasn't mentioned anywhere in the tutorial.
$config['base_url'] = base_url('posts/browse');
$config['prefix'] = '/page/';
$config['suffix'] = '/item_per_page/1';
$config['uri_segment'] = 4;
this can generate urls with page number in the middle of the url.
eg. http://www.blah.com/posts/browse/page/2/item_per_page/1;
The documentation clearly explains how to do this.
Short version: use $config['uri_segment'] = 4; in your pagination config. uri_segment tells the pagination class which uri segment contains the page #.

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

Codeigniter Pagination URL issue

I just implemented pagination in my website www.reviewongadgets.com
When I click the home page it's divided into two pages which is correct but when I click on next link it shows the URL as
http://www.reviewongadgets.com/home/10
which is also correct but when I come back to previous page number it displays URL as
http://www.reviewongadgets.com/home/home/
So can you please help resolve this problem? Below is the controller snippet where $url is http://www.reviewongadgets.com/home
$config['base_url'] = $url;
$config['total_rows'] = $this->MiscellaneousModel->countEntries();
$config['per_page'] = 10;
$base_url = site_url('/');
$config['uri_segment'] = '2';
//$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
This should work for you(famous last words)
//Add this to your routes
$route['home/(:num)'] = 'home/index/$1';
public function index($offset=0){
$limit = $this->config->item('pagination_per_page'); // default 10
//find data based on limits and offset
$query = //you query LIMIT = $limit OFFSET = $offset
$count = //count the number of rows returned by $query
//init pagination attributes
$config = array(
'base_url' => site_url('home'),
'total_rows' => $count,
'per_page' => $limit,
'uri_segment' => 2
);
$this->pagination->initialize($config);
//load the view and pagination data
$this->load->view('some_view', array(
'pagination' => $this->pagination->create_links(),
'data' => //data return from $query as object or array
));
}
The problem might be on how you define the base_url, since you give it a variable but you didn't tell where and how you assign a value to it. Try with:
$config['base_url'] = site_url('home');
$config['total_rows'] = $this->MiscellaneousModel->countEntries();
$config['per_page'] = 10;
$config['uri_segment'] = '2';
Since you're using the index method, maybe specifying it can solve the issue (I had once a similar problem and that did work)
$config['base_url'] = site_url('home/index');

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

Resources