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
Related
I get 404 page when i go to any page
Routes.php
$route['default_controller'] = "home";
$route['default_controller/page/(:any)'] = 'home/$1';
Controller (home.php)
public function index($ppage=1)
{
$this->load->library('pagination');
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 2;
$config['num_links'] = 5;
$config['base_url'] = 'http://www.test.com/page/';
$config['first_url'] = 'http://www.test.com/page/1';
$this->pagination->initialize($config);
}
When i go to the URL http://www.test.com/page/1 or http://www.test.com/page/2 i get 404 no found. Where did i go wrong with this line?
$route['default_controller/page/(:any)'] = 'home/$1';
Default controller isn't a variable:
$route['home/page/(:any)'] = 'home/$1';
or if that doesn't work:
$route['page/(:any)'] = 'home/$1';
Kindof a weird approach imo.
I've got a method news() which takes two optional parameters - $category & $slug.
The news page needs to show a paginated list of all of the uncategorised news articles (the Category page (with $category set) will need to do the same, but for the categorised subset).
Because of this, it seems that the standard pagination isn't working, as the 2nd URI segment is being seen as the $category parameter for news(). Is it possible to work around this, perhaps treating the 2nd URI segment as the $category parameter if it isn't an integer, or the pagination parameter if it is?
Here are the relevant code pieces:
Controller
function news($category = null, $slug = null) {
if($category == null) { // Get the standard "news" page
// Define the pagination config
$config = array();
$config['base_url'] = base_url() . 'news/';
$config['total_rows'] =$this->post_model->count_posts('NWS');
$config['per_page'] = 3;
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->load->library('pagination');
$this->pagination->initialize($config);
// Set the page info
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['newsPosts'] = $this->post_model->get_post_list_excerpt('NWS',$config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$this->template->load('default', 'newsview', $data);
}
elseif($slug == null) {
// Get the page specific to the chosen category
}
}
To try and tidy up the URLs, I'm also using routing:
routes.php
$route['news'] = 'site/news';
$route['news/(:any)'] = 'site/news/$1';
$route['news/(:any)/(:any)'] = 'site/news/$1/$2';
Is there a way round what I'm trying to do/is it even possible? I'd like to avoid having to have separate methods/controllers (such as news/categories/$category if possible
OK, this is some advice that you can consider.
You could use base_url("site/news/"); instead of base_url() . 'news/'; to give clarify to your code.
Make use of news/(:any)/(:any) regex at this case is ambiguous/incorrect because the first (:any) pattern is already containing all the rest of the url. What I mean:
example.com/site/news/12/file
$route['news/(:any)'] = 'site/news/$1';
$1 will match 12/file
$route['news/(:any)/(:any)'] = 'site/news/$1/$2';
$1 will match: 12/file
$2 will match: (nothing)
Might you can conside to use some specific wildcard and give extra security to your urls:
Note: Remember to apply the rules from longest to shortest:
$route['news/(:num)/([a-z]+)'] = 'site/news/$1/$2';
$route['news/(:num)'] = 'site/news/$1';
$route['news'] = 'site/news';
Now, coming back to the original question, I think you could inverse the params to let the category as last one. Let's see:
$config['base_url'] = base_url("news/category/subset");
$config['uri_segment'] = 4;
Take a look at this:
public function news($category = false, $subset = false, $page = 1)
{
$this->load->library('pagination');
//checks if category is the page number
if ((string)(int)$category === $category)
{
//ok, there is not category neither subset
$config['base_url'] = base_url('site/news');
$config['uri_segment'] = 3;
}
//checks if subset is the page number
else if ((string)(int)$subset === $subset)
{
$config['base_url'] = base_url('site/news/' . $category);
$config['uri_segment'] = 4;
}
//by elimination, all the three parameters are presents
else
{
//ok, both are presents
$config['base_url'] = base_url('site/news/' . $category . '/' . $subset);
$config['uri_segment'] = 5;
}
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
// more stuff here
}
This pagination config should works with urls like:
example.com/site/news/
example.com/site/news/cat/
example.com/site/news/cat/subset/
and page numbers:
example.com/site/news/3
example.com/site/news/cat/5
example.com/site/news/cat/subset/3
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
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 #.
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";