Hi have am new to codeigniter and have been struggling with CI pagination library for days.
I have this app where all pagination are working fine except for skipping data by offset.
i.e. when I click on next, It only shifts data by 1 record only. Example: From 1-10 showing it goes to 2-11 and so on..
Here is my controller:
$active = 1;
$total = $this->all_users_model->total_number_of_rows();
$per_page = 10;
$offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$config['base_url'] = base_url().'admin/manage-users/';
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = 'First';
$config['prev_link'] = 'Previous';
$config['next_link'] = 'Next';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$data['admin_all_users'] = $this->all_users_model->show_all_user_details($active, $per_page, $offset);
Model:
public function show_all_user_details($active,$per_page,$offset) {
$this->db->where(array('activated'=>$active,'admin !='=>1));
$this->db->limit($per_page,$offset);
$this->db->order_by('api_id','desc');
$query=$this->db->get('registered_members');
return $query->result();
}
It's because your offset is incrementing only with one at a time: page/1, page/2, also as your db query limit. Currently, your db limit is $this->db->limit(10, 1);, $this->db->limit(10, 2);.
This shows 10 records from record 1, and then from record 2.
Just change the limit in your model to:
$this->db->limit($per_page, $offset * $per_page);
Related
Using codeigniter 3 I have setup pagination. When I click on the next link I get the same results minus the first post, I cannot seem to find what is causing this.
I tried removing the offset and not allowing numbers, and I still have no change.
controller
public function index($offset = 0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = 'Newest';
$data['posts'] = $this->post_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
model
public function get_posts($slug = FALSE, $limit=FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($slug === FALSE){
$this->db->order_by('created_time','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
view
<ul class="pagination">
<?php echo $links; ?>
</ul>
///////////////////UPDATE/////////////
So this is how my code looks now after updating as your said.
public function index($offset=0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['num_links'] = 10;
$config['per_page']=3;
$limit = $config['per_page'];
$offset = ($offset) * ($config['per_page']);
this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_posts(FALSE,$limit,$offset);
in the view I have it set to
<?php echo $this->pagination->create_links(); ?>
So now the issue is, it shows 3 rows(results) and when I click next it shows nothing and that's it, so now I am only getting 3 results only on the first page and none on the next pages. What is wrong?
Index Function Not Working Segment Use Query String
if $offset is current_page_number starting on 0, definition should be:
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
If you define offset as current_page, starting on 1.
public function index($offset = 1){
......
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset - 1) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
Talking about "Index Function Not Working Segment Use Query String"...
If you have $config['enable_query_strings'] = TRUE you should GET it
public function index() {
$page = $this->input->get('page');
...
$offset = ($page - 1) * ($config['per_page']);
...
I using GET method for searching records. now when i have applied pagination i can't understand how to submit data to next pagination page link.
I have tried this answer. but it is just stacking page number in link segments.
example:
From
localhost/phc/search/results/0?s=ai&product_cat=all&post_type=product
To
localhost/phc/search/results/?s=ai&product_cat=all&post_type=product/12/24
so i didn't used that code.
And now my code is:
public function results() {
//pagination
$offset = $this->uri->segment(3);
$limit = 12;
//$offset = 0;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$config['first_url'] = base_url() .'search/results/0';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Tell what the best way to submit data in this case if not GET and how can i access this data.
Put offset in your url
localhost/phc/search/results?s=ai&product_cat=all&post_type=product&offset=123
and set these configs.
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
I changed your code but it's not tested at all.
public function results() {
//pagination
$offset = $this->input->get('offset') ? $this->input->get('offset') : 0;
$limit = 12;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
// set these mostly...
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Cannot access protected property `CI_Pagination
Trying to add submit button on last page of pagination currently it's on every page by default...on line 49 i have condition to check whether i am in last page or not, I want a submit button at the end of the pagination page
Code on which error is showing..please explain how to implement
if($this->pagination->cur_page >= ceil($this->pagination->total_rows / $this->pagination->per_page))
{
$isLastPage = true;
}else{
$isLastPage = false;
}
Pagination code
public function quizdisplay()
{
//echo $this->pagination->create_links();
//$this->load->library('pagination');
$config = array();
$config['base_url'] = 'http://localhost/xampp/cii/index.php/Questions/quizdisplay';
//$config['total_rows'] = 10;
$total_row = $this->quizmodel->record_count();
$config["total_rows"] = $total_row;
$config['per_page'] = 1;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}else{
$page = 1;
}
$data["results"] = $this->quizmodel->getQuestions($config["per_page"], $page);
$this->pagination->create_links();
$data["links"] = $this->pagination->create_links();
View data according to array.
$this->load->view("play_quiz", $data);
}
Both the codes are in same function
I am having a pagination page in my controller, i am kind of puzzled in listing 2 data per page on my view. The controller code which i am using is as follows:
public function newsletter()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/newsletter";
$this->load->model('newsletter_model');
$total_row = $this->newsletter_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 2; // per page 2 records
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['page_query_string'] = FALSE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$this->load->model('newsletter_model');
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('newsletter_model');
$this->load->view('newsletter/newsletter',$data);
}
With the above code i am getting 2 records per page as i intended, but i could not understand the logic behind the pagination. can anyone explain me the logic worked in codeigniter pagination, with my work itself as it would be handy for me to understand it.
My Model code is as follows:
public function fetch_data($limit, $id)
{
$this->db->select('*');
$this->db->from('ins_newsletter');
$this->db->order_by('nl_id', 'desc');
$this->db->limit($limit,$id);
$query = $this->db->get();
return $query->result_array();
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
CI's Paginator class only generates pagination links for you.
The "record pagination" logic happens here:
$this->db->limit($limit, $offset);
$limit is the number of records to fetch, $offset is the offset of the first row to return.
To fetch the first two records, we will set $limit = 2, $offset = 0
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 0, 2;
To fetch record number 3 and 4, we will set $limit = 2, $offset = 2
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 2, 2;
Edit:
There is a small bug in your code.
The offset should be 0 for page 1, 2 for page 2, 4 for page 3, 6 for page 4, 8 for page 5...and so on.
public function fetch_data($limit, $page)
{
$offset = ($page - 1) * $limit;
...
}
And I would suggest you to change the second param to $page instead of $id.
I'm using codeigniter 3 and using hmvc for admin but problem pagination no showing First a Last link.i could not understand where is the problem
$config['per_page'] = 2;
$config['base_url'] = base_url().'admin/manage-cms';
$config['num_links'] = 20;
$config['uri_segment'] = 3;
$page = $this->uri->segment(3);
$limit_end = ($page * $config['per_page']) - $config['per_page'];
if ($limit_end < 0){
$limit_end = 0;
}
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$data['count_cms']= $this->cms_model->count_cms();
$config['total_rows'] = $data['count_cms'];
$data['cms'] = $this->cms_model->get_cms($config['per_page'],$limit_end);
$this->pagination->initialize($config);
i also updated the code to
$config['per_page'] = 2;
$config['base_url'] = base_url('admin/manage-cms');
$config['num_links'] = 20;
$config['uri_segment'] = 3;
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$data['count_cms']= $this->cms_model->count_cms();
$config['total_rows'] = $data['count_cms'];
$data['cms'] = $this->cms_model->get_cms($config['per_page'],$page);
$this->pagination->initialize($config);
But following problem have found
1)in url, pagination coming like url/2,url/4,url/6 etc
But i trying to show url/1,url/2,url/3
2) I have inserted 9 records in db but still First and Last link is not displayed