Codeigniter Pagination having page number in the middle of url - codeigniter

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 #.

Related

Need understanding $config["uri_segment"] related code in CodeIgniter

Hey CodeIgniter developers I am new in codeIgniter please see my code related to pagination. Pagination is working fine. I just need your help to understand few lines of code, please see the commented lines in code where I just need your help to understand it.
public function example1() {
$config = array();
$config["base_url"] = base_url() . "welcome/example1";
$config["total_rows"] = $this->services->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3; // Need help on this line
$config["next_link"] = '>';
$config["prev_link"] = '<';
$this->pagination->initialize($config);
// Need help on this if condition blocks
if ($this->uri->segment(3)) {
$page = ($this->uri->segment(3));
} else {
$page = 1;
}
$data["results"] = $this->services->fetchServicesByPagination($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
Agree With Javier Larroulet
From the Codeigniter documentation $config['uri_segment'] defines what URI segment will contain the page number. It defaults to 3, but you may use another segment if you need. The $this->uri->segment(3) condition on the if clause is checking if the URI Segment number 3 (first after the method name) is set or not. If it is set, use its value as page number, otherwise, default to page 1. Reference

Is it possible to have the same CodeIgniter URI segment as a function parameter and a pagination parameter?

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

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');

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