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.
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 have an issue with codeigniter pagination, the same technique i did in the older codeigniter version it works fine but not in this latest version(3.1.9). The first front page fetch data fine but when i click on page 2, 3 or 4 still it display those of the first front row data.
Here is my controller:
$total_rows = $this->estate_model->estate_count();
$config = pagination_configuration(base_url("real_estate"), $total_rows, 10, 3, 5, true);
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
$page_num = $page-1;
$page_num = ($page_num<0)?'0':$page_num;
$page = $page_num*$config["per_page"];
$data["links"] = $this->pagination->create_links();
$obj_result = $this->estate_model->get_all_active_estate($config["per_page"], $page);
Model:
public function get_all_active_estate($limit, $start)
{
$this->db->select('*');
$this->db->from('estate');
$this->db->where('status', 'active');
$this->db->limit($limit, $start);
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
Please help.. thank you
try
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
instead
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
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);
I have set rout as
$route['dog/(:any)'] = "dog/index/$1"; /// for single dog info
$route['dog/list'] = "dog/listing"; /// for dog list, display all dogs.
$route['dog/list/(:num)'] = "dog/listing/$1"; /// for pagination
single dog url is like dog/dogName-4.html
my controller is as
public function index()
{
$dogInfo = $this->uri->segment(2);
if ($dogInfo != "")
{
$dogDetails = explode('-', $dogInfo);
$this->load->view('common/header',$header);
$this->load->view('dog/dog_info', $content);
$this->load->view('common/footer', $footer);
}
else
redirect('welcome', 'location', 301);
}
public function listing()
{
$this->load->library("pagination");
$breed = $this->input->get('breed');
$gender = $this->input->get('gender');
$state = $this->input->get('state');
$seller = $this->input->get('seller');
$config = array();
$config["base_url"] = base_url() . "dog/list/";
$config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
$config["per_page"] = 5;
$config["uri_segment"] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);
$content['paginatonLinks'] = $this->pagination->create_links();
$content['total_dogs'] = $config['total_rows'];
$content['cur_page'] = $page + 1;
$content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);
$this->load->view('common/header');
$this->load->view('dog/dog_list', $content);
$this->load->view('common/footer', $footer);
}
The controller's index function is to display information about one dog, and listing function is for all the dogs,
I have to set pagination for listing function i did set all the required variables for pagination, pagination displaying the result as well
Found [134] Ads :: Page 1 of 27 1 2 3 > Last ›
but when i click on the pagination 1 2 3 else on page it brings me to the index page.
I need to be on the listing function of the controller. please any one help me.
here is the updated code
the rout code is
$route['dog/list'] = "dog/listing";
$route['dog/list/(:num)'] = "dog/listing/$1";
$route['dog/(:any)'] = "dog/index/$1";
and here is the updated controller code
$config = array();
$config["base_url"] = base_url() . "dog/list/";
$config['suffix'] = '?'.http_build_query($_GET, '', "&");
$config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
$config["uri_segment"] = 3;
$config["per_page"] = 5;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['cur_tag_open'] = '<a href="#" class="page_selected">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$this->load->model('DogsListing_model', 'dogs');
$this->load->model('Breed_model', 'breeds');
$content['breeds'] = $this->breeds->get_all_id_title();
$content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);
$content['search'] = array('breed' => $breed, 'gender' => $gender, 'state' => $state, 'seller' => $seller);
$content['paginatonLinks'] = $this->pagination->create_links();
$content['total_dogs'] = $config['total_rows'];
$content['cur_page'] = $page + 1;
$content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);
i have found the solution of my question
first i have use $this->uri->segment(3) and $config["uri_segment"] = 3; i was using a wrong uri->segment for pagination. this solve my problem related to pagination.
my next part is i have to set variables for seller or search results with the pagination. for example i have the url doglist.html/?seller=29 i need to embed pagination with this pagination. so that the result will be like dog/list/3?breed=&gender=female&state=&submit2=Fetch i have solve this issue with this line of code $config['suffix'] = '?'.http_build_query($_GET, '', "&"); now my pagination with variables works fine.
Thanks to #jtheman and #Venkat, but mostly #jtheman for their contributings.
If you have any doubt related to pagination check out this generalised function which i wrote as answer to another question.
There is a bit of problem in my code which i am not able to solve.
I m using CI 1.7.2.
I have implemented the CI Pagination into the system correctly.
The results are displayed fine but the links in the pagination are not rendering correctly.
For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.
Here is the code that has been implemented
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
$result = $this->bmodel->getAllBanners($perPage,$uri_segment);
$data['result'] = $result;
thanks in advance.
J
Heyy,
I also faced the same problem. In the end, solution turned out to be very simple. :)
by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
$config['uri_segment'] = 3; /* segment of your uri which contains the page number */
$this->pagination->initialize($config);
Hope this solves your problem
ok... try this...
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
/* Comment out this part
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
*/
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
/*Change the following line*/
$result = $this->bmodel->getAllBanners($perPage,$this->uri->segment(5));
$data['result'] = $result;
$this->load->library('pagination');
$config['base_url']="http://localhost/CodeIgniter/pagination";
$config['per_page']=2;
$config['total_rows']= $this->db->get('record')->num_rows();
$this->pagination->initialize($config);
$data['query']= $this->db->get('record',$config['per_page'], $this->uri->segment(3));
$this->load->view('pagination',$data);
$config['uri_segment'] = num; /* where num is the uri segment where you have page number */
Try this, it might help.
class Admin_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_product($search, $page, $perpage) {
$page = $page - 1;
$page < 0 ? $page = 0 : $page = $page;
$from = $page * $perpage;
$query = $this->db
->select('*')
->from('tbl_product')
->limit($perpage, $from)
->get();
return $query->result();
}
}