Codeigniter default_controlller and pagination - codeigniter

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.

Related

How to Implement Codeigniter Default pagination with pjax

I want to implement Pjax jquery library with codeigniter Other function just works fine. But when i added it woth pagination and its dont works. when i Click pagination button than its change url but suddenly after its reloading with normal php not ajax..
$this->load->library('pagination');
$FilterData = $this->input->get();
$config = bootstrapPagination();
$config['base_url'] = main_url('members');
$config["total_rows"] = $this->member->browse_search_total($FilterData);
$config["per_page"] = PER_PAGE;
$config['reuse_query_string'] = TRUE;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data['members'] = $this->member->browse_search($FilterData, PER_PAGE, $offset);
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
if (isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == TRUE)
{
$this->load->view('show_members', $data);
} else
{
$this->output->set_template('frontend');
$this->output->set_title('Members | ' . sitename());
$this->load->view('show_members', $data);
}
I solved the problem with:
$.pjax.defaults.timeout = 3000;
Just try the below config value
$config['page_query_string'] = TRUE
How can you get $config["total_rows"]??
I think it should $config["total_rows"] = $this->member->browse_search_total($FilterData)->num_rows();

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 - 404 Error

I am trying to use pagination for blog articles on my homepage:
Controller:
public function index()
{
$data['view'] = "home";
/** Pagination **/
$config['base_url'] = base_url().'home/';
$config['total_rows'] = $this->Blog_model->count_articles();
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data['paginate'] = $this->pagination->create_links();
$data['articles'] = $this->Blog_model->get_articles($config['per_page'],$this->uri->segment(2));
$this->load->view('template', $data);
}
Everything seems to work ok with the information retrieval and the pagination however, when I click on the number links or next links I get a 404 Not Found error.
I am assuming that this has something to do with the URI segment?
The URL that is being loaded is http://www.example.com/home/2 for the second page.
You can also add a routing rule like:
$route['home/(:num)'] = 'yourhomecontroller';
then you can use it without index or any method, the num tells it to route any url with a number after home/ to index of home
Codeigniter pages are of the form http://domain.com/controller/method/arguments.
If you leave out the method the default one will load, but if you need to pass an argument, you'll have to put the method as it comes before.
http://www.example.com/home/index/2
controller code
public function index()
{
$home=$this->uri->segment(2);
$limit_ti=2;
if(!$home):
offset_ti = 0;
else:
$offset_ti = $home;
endif;
$this->load->model('Blog_model');// call model
$this->load->library('pagination'); // call library
$query=$this->Blog_model->get_articles($limit_ti,$offset_ti); // geting aan article
$total_page = $this->Blog_model->count_articles(); // count row
/** Pagination **/
$config['base_url'] = base_url().'index.php/home/'; // assuming you doesn't have htaccess
$config['total_rows'] =$total_page->num_rows();
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data = array('query' => $query,'page'=>$home);
$data['view'] = "home";
$this->load->view('template', $data);
}
model code
function get_articles($limit,$ofset){
$query=$this->db->query("select * from *table* order by id ASC LIMIT $ofset,$limit");
return $query;
}
function count_articles(){
$query=$this->db->query("select * from table");
return $query;
}
in view
<?php echo $this->pagination->create_links(); // call the pagnation?>

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