Codeigniter Pagination - 404 Error - codeigniter

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

Related

When I click to the next number on pagination, Link active still at number 1

My controller function
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_media');
$this->load->library('pagination');
}
public function index()
{
$config['base_url'] = base_url()."secret/news/index";
$config['total_rows'] = $this->news_media->getCount();
$config['per_page'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4))?$this->uri->segment(4):0;
$data = array();
$data['allnews'] = $this->news_media->get($config['per_page'],$page);
$data['links'] = $this->pagination->create_links();
$html = array();
$html['title'] = "Setting";
$html['head'] = $this->load->view('secret/template/header',null, true);
$html['top'] = $this->load->view('secret/template/top',null, true);
$html['menu'] = $this->load->view('secret/template/menu',null, true);
$html['content'] = $this->load->view('secret/news/news',$data, true);
$html['js'] = $this->load->view('secret/template/js',null, true);
$this->load->view('secret/template/template',$html);
}
}
Problem is that I have pagination 1,2 and in every page I display 3 items.
When I click second page link active still in number 1. How to fix its?
Thank before
Add uri_segment in you pagination config settings:
$config['base_url'] = base_url()."secret/news/index";
$config['total_rows'] = $this->news_media->getCount();
$config['per_page'] = 3;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
Hope this will help you to resolve this problem. Take a look on documentation that explain it.
$config['uri_segment'] = 3;
The pagination function automatically determines which segment of your URI contains the page number. If you need something different you can specify it.

Codeigniter pagination is not passing the correct link value in url

I have created a pagination in codeigniter, In that I have totally 13 records and i want to display 5 records in each page. When i click on the pagination link number 2,It should pass value 2 in url and it show the next 5 records but I am getting value 5 in url instead of 2.
My controller code:
public function activities($page_num = 1){
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num))
$page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);
$data['activity_list'] = $this->skill_model->all_activities(1,$limit_start,$limit_end);
}
This is my view code:
<?php echo '<div class="pagination" style="float:right;">'.$this->pagination->create_links().'</div>'; ?>
My Model part:
function all_activities($flag,$limit_start,$limit_end){
$this->db->select('*');
$this->db->from('skills_activities');
//echo $limit_start." ".$limit_end;
if($flag == 1 ){
$this->db->limit($limit_start, $limit_end);
$query = $this->db->get();
return $query->result_array();
} else {
$query = $this->db->get();
return $query->num_rows();
}
}
When i click on pagination link I am getting the following url:
http://192.168.1.97/projects/homecare/admin/skills/activities/5
but I should get:
http://192.168.1.97/projects/homecare/admin/skills/activities/2
I don't know where i have done a mistake.
can anybody help me?
Thanks in advance.
$config['use_page_numbers'] = true;
This will produce page number in the url instead of record number. By default its false.
So you controller will look like this
public function activities($page_num = 1)
{
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['use_page_numbers'] = true;//you missed this line
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num)) $page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);

Codeigniter pagination links dosnt work after the first page

i have a search field and i want to show the results with pagination.
every thing works well in first result page but in other pages there is noting to show without any errors and when back to first page also there is noting.
i putted the posted key in session to avoid losing it.
but still noting.
this is my controller:
public function search()
{
$this->session->set_userdata('searched',$this->input->post('searchterm'));
$searchterm = $this->session->userdata('searched');
$limit = ($this->uri->segment(3) > 0)?$this->uri->segment(3):0;
$config['base_url'] = base_url() . 'home/search';
$config['total_rows'] = $this->home_model->search_record_count($searchterm,$language);
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['display_pages'] = TRUE;
$choice = $config['total_rows']/$config['per_page'];
$config['num_links'] = 3;
$this->pagination->initialize($config);
$data['results'] = $this->home_model->search($searchterm,$limit,$language);
$data['links'] = $this->pagination->create_links();
$data['searchterm'] = $searchterm;
$data['total']= $this->home_model->search_record_count($searchterm,$language);
putHeader();
putTop();
putTopmenu();
putSearch($data);
putFooter();
}
this is my model:
public function search_record_count($searchterm)
{
$sql = "SELECT COUNT(*) As cnt FROM content WHERE body LIKE '%" . $searchterm . "%'";
$q = $this->db->query($sql);
$row = $q->row();
return $row->cnt;
}
public function search($searchterm,$limit)
{
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','noor_content.category = categories.id')
->like('noor_content.title', $searchterm)
->like('noor_content.body', $searchterm)
->limit("5")
->order_by("content.id","DESC")
->get();
if($data->num_rows() > 0)
{
return $data->result();
//print_r($data);exit();
}
else
{
return 0;
}
}
The problem is that you are not limiting your query according to the pagination. In your controller you are getting the 3rd URL segment
$limit = ($this->uri->segment(3) > 0)?$this->uri->segment(3):0;
And passing it to you model but then you are not doing anything with it. In your model you want to do something like
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','noor_content.category = categories.id')
->like('noor_content.title', $searchterm)
->like('noor_content.body', $searchterm)
->limit("10, " . $limit) //Edited this line
->order_by("content.id","DESC")
->get();
i think you should use method = 'get' instead of post... i mean some thing like this ..
<form method="get" action="your_controller/your_function">
//your search form
</form>
by using get instead of post u dont have to use any session variables and u can easily access them in your controller as
$this->input->get('feild_name');
moreover u wont have problem with second page or any other page in pagination as all the search feilds get attached in the url and u can access it easily using $this->input->get('feild_name'); untill your url is changes to something else.. i will aslo save u al ot of coding ..

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

pagination codeigniter always starts at page 2 and active link is always at page 2

i'm new to codeigniter and i'm using codeigniter pagination by getting query result from Category Id, but the page always starts at page 2 .
how do i fix the pagination to set the start page at number page 1?
public function kategori($kat=''){
$config['base_url'] = base_url().'index.php/lowongan/kategori/'.$kat;
$data['db']=$this->query->katPostingan($kat);
$config['total_rows'] = $data['db']->num_rows();
$config['per_page'] = 1;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data2['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$data2['records'] = $this->query->katPos($kat, $config['per_page'], $page);
$this->load->view( 'user/hal_kategori',$data2);
}
and my model
public function katPos($kat='',$limit,$start){
$this->db->where("id_kat", $kat);
$this->db->limit($limit,$start);
$query = $this->db->get("showKat");
return $query;
}
public function katPostingan($kat=''){
$this->db->where("id_kat", $kat);
$query = $this->db->get("showKat");
return $query;
}
try a different way of solution,
controller(index())
$this->load->library('pagination');
$config['base_url'] = site_url().'classname/index';
$config['per_page'] = $this->config->item('pagination_number');
$config['uri_segment'] = 4;
$data['count']=$this->example_model->count();
$config['total_rows'] =$data['count'];
$data['classname']=$this->example_model->getall($this->config->item('pagination_number'),$this->uri->segment(4));
$this->pagination->initialize($config);
$data['page_link']=$this->pagination->create_links();
$this->load->view('users/classname.php',$data);
model
function getall($num,$offset)
{
$this->db->from('table_name');
$this->db->limit($num,$offset);
$query = $this->db->get();
return $query->result_array();
}
function count()
{
$this->db->from('table_name');
$query = $this->db->get();
$rowcount = $query->num_rows();
return( $rowcount);
}
View
<div class="pagination"> <?php echo $page_link;?> </div>

Resources