How to show continued numbered of data with pagination - codeigniter

I am trying to display data in an ordered list with pagination in codeigniter. The numbered works fine in first page number 1 until limit, but in the next page it counts down start to 1, not continuing from previous page.
here is the controller code :
public function index()
{
// init params
$params = array();
$limit_per_page = 2;
$start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$total_records = $this->model_pesan->get_total();
$data['setting'] = $this->setting_model->get_file();
if ($total_records > 0)
{
// get current page records
$params["results"] = $this->model_pesan->get_current_page_records($limit_per_page, $start_index);
$params["ruangan"] = $this->ruangan_model->get_file();
$config['base_url'] = base_url() . 'home/index';
$config['total_rows'] = $total_records;
$config['per_page'] = $limit_per_page;
$config["uri_segment"] = 3;
$config['full_tag_open'] = '<nav><ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tagl_close'] = '</span></li>';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
// build paging links
$params["links"] = $this->pagination->create_links();
}
$this->load->view('template/header_user', $data);
$this->load->view('user/home', $params);
$this->load->view('template/footer_user', $data);
}
and this is the model code :
public function get_current_page_records($limit, $start)
{
$query = $this->db->query('set #row_number = 0');
$this->db->select('(#row_number:=#row_number + 1) as num ,reservasi.id_reservasi,DATE_FORMAT(reservasi.tanggal, "%d-%m-%Y") as tanggal, reservasi.acara, reservasi.waktu_mulai, reservasi.waktu_selesai, reservasi.keterangan, ruangan.nama_ruangan, user.nama, reservasi.organisasi, reservasi.notelp');
$this->db->from('reservasi');
$this->db->join('user', 'reservasi.id_user = user.id');
$this->db->join('ruangan', 'reservasi.id_ruangan = ruangan.id_ruangan');
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
public function get_total()
{
return $this->db->count_all_results('reservasi');
}
the ouput should be =
first page :
1
2
3
4
then in the next page :
5
6
7
8

Try to set the row_number to $start+1 instead of 0 on the model, so it will not start from 0 again on every page requested :
$query = $this->db->query('set #row_number = '.$start+1);

Related

Why do i get the same values in all pages in CI pagination?

I'm kinda new in CI and i'm trying to create a proper pagination for my page.
I have the following model and controller and even if i manage to get the pagination links, when i visit the other pages i get the same entries again and again.
I also have applied an Ajax "GET" filtering for fetching results from db and i managed to reuse the url parameters when i change pages.
The problem is that i don't know if i should try to create an infinite scroll function or maintain the default CI pagination.
I'd like to hear your opinions too!
Thank you in advance!
public function get_filtered_pets($limit, $start){
$this->db->order_by('pet_created_at', 'DESC');
$this->db->join('users', 'users.user_id = pets.pet_user_id');
$this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
$pet_data = array();
if ( $this->input->get('petStatus') ) {
$pet_data['pet_status'] = $this->input->get('petStatus');
}
if ( $this->input->get('petCategory') ) {
$pet_data['category_id'] = $this->input->get('petCategory');
}
if ( $this->input->get('petGender') ) {
$pet_data['pet_gender'] = $this->input->get('petGender');
}
if ( !empty($pet_data) ) {
$this->db->where($pet_data);
}
$query = $this->db->get('pets');
if ($start > 0) {
$this->db->last_query();
}
return $query->result_array();
}
And my controller
public function index(){
$query = $this->db->get('pets');
$pet_num = $query->num_rows();
$config = array();
$config["base_url"] = base_url() . "pets/index/";
$config["total_rows"] = $pet_num;
$config["per_page"] = 12;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$config['attributes'] = array('class' => 'page-link');
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_link"] = "«";
$config["first_tag_open"] = "<li>";
$config["first_tag_close"] = "</li>";
$config["last_link"] = "»";
$config["last_tag_open"] = "<li>";
$config["last_tag_close"] = "</li>";
$config['next_link'] = '>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '<li>';
$config['prev_link'] = '<';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '<li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pets'] = $this->pet_model->get_filtered_pets($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
//View files
$this->load->view('templates/header', $data);
$this->load->view('pets/index', $data);
$this->load->view('templates/footer', $data);
}
You are not using the value of $limit and $start nowhere. See Limiting or Counting Results.
You should:
public function get_filtered_pets($limit, $start){
$this->db->limit($limit, $start);
/* ... */
return $query->result_array();
}
And also, when calling this function you have to multiply: per_page * page, to get the offset (starting index).
$this->pet_model->get_filtered_pets($config["per_page"], $page * $config["per_page"])
To count results correctly, I normally have two functions on model: "get($search, $offset)" and "count($search)".
The function count has exactly the same code on get, except the select fields.
See this code, I think you will get it: https://pastebin.com/dZG90Lzm.
ps: There's ways to optimize it and avoid duplicate code.

Codeigniter sort by price pagination not working from page 2 onwards

I'm facing some issue with CI pagination. The sorting are all good now but when I try to navigate to page 2 onwards, the sorting of the listing will then be "restarted". Restarted I mean, to go back to the first sorting method when I first load the page.
Sort options: Most recent, price low to high & price high to low
Page Controller:
public function index() {
$query_string = $this->input->server('QUERY_STRING');
$page_start = $this->input->get('page');
$products = $this->Marketplace_model->get_products($query_string);
$data['products'] = array_slice($products, $page_start, 12);
$this->_doPagination($data['products'][0]["total_rows"], $test);
$data['sorts'] = array("recent" => "Most Recent", "lowtohigh" => "Price Low To High", "hightolow" => "Price High To Low");
$data['sortselected'] = get_sortby();
$data['selectedSortBy'] = $query_string;
$this->template->set('title', 'Marketplace');
$this->template->load('default_layout', 'contents', 'index', $data);
}
Pagination controller:
private function _doPagination($total_rows = NULL) {
$per_page = get_perpage();
$this->load->library('pagination');
$config['total_rows'] = ($total_rows != NULL) ? $total_rows : 0;
$config['per_page'] = ($per_page != NULL) ? $per_page : 12;
$config['num_links'] = 2;
$config['use_page_numbers'] = TRUE;
$config['base_url'] = '';
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
$config['full_tag_open'] = '<ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = '«';
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['first_url'] = '?page=1&';
$config['last_link'] = "»";
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = false;
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = false;
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
}
Model:
public function get_products($query_string) {
if ($query_string == 'recent') {
$sql = "select total_rows=(select count(*) from o2o_pit_live_product)
, * from o2o_pit_live_product order by supplier_on DESC";
} elseif ($query_string == 'lowtohigh') {
$sql = "select total_rows=(select count(*) from o2o_pit_live_product)
, * from o2o_pit_live_product order by regular_price ASC";
} elseif ($query_string == 'hightolow') {
$sql = "select total_rows=(select count(*) from o2o_pit_live_product)
, * from o2o_pit_live_product order by regular_price DESC";
} else {
$sql = "select total_rows=(select count(*) from o2o_pit_live_product)
, * from o2o_pit_live_product";
}
$result = $this->db->query($sql)->result_array();
if ($result) {
return $result;
}
return NULL;
}
Views:
<select name="filter" class="custom-select filter" id="filter" onchange="location = this.value;">
<?php foreach ($sorts as $skey => $sname): ?>
<option value="?<?php echo $skey; ?>"
<?php
if ($selectedSortBy === 'recent' && $skey === 'recent') {
echo 'selected';
}
if ($selectedSortBy === 'lowtohigh' && $skey === 'lowtohigh') {
echo 'selected';
}
if ($selectedSortBy === 'hightolow' && $skey === 'hightolow') {
echo 'selected';
}
?> >
<?php echo $sname; ?>
</option>
<?php endforeach; ?>
</select>
Please assist me. Thanks in advance.
array_slice second parameter is set for from where records is start.you set page number instead of from where records start.so your code should be as per below
$start = ($page_start-1)*get_perpage();
array_slice($products,$start,get_perpage());

pagination doesn't work in codeigniter

I made an optional search that makes the user search with username or customer or date from/to the result returning well to the first page but if I click on another page the result return nothing
this my front and all that input optional
i think the problem the pagination path because if i fill all inputs the pagination path become
controllername/userid/customerid/fromdate/todate
and if skip any input I find the pagination path is missing that
this my controller
public function search($page_num = 1){
$this->load->library('pagination');
$this->load->helper("url");
$this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
$username=$this->input->post('select_user');
$username = ($this->uri->segment(3)) ? $this->uri->segment(3) : $username;
$customer=$this->input->post('select_customer');
$customer = ($this->uri->segment(4)) ? $this->uri->segment(4) : $customer;
$datefrom=$this->input->post('from');
$datefrom = ($this->uri->segment(5)) ? $this->uri->segment(5) : $datefrom;
$dateend=$this->input->post('to');
$dateend = ($this->uri->segment(6)) ? $this->uri->segment(6) : $dateend;
$total_row = $this->CustomReport_m->search_count($username,$customer,$datefrom,$dateend);
//$config['use_page_numbers'] = TRUE;
$config['base_url'] = site_url("CustomReport/search/$username/$customer/$datefrom/$dateend");
$config['total_rows'] = $total_row;
$config['per_page'] = 10;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
if ($this->uri->segment(7)) {
$page = ($this->uri->segment(7));
} else {
$page = 1;
}
$str_links = $this->pagination->create_links();
$data["links"]= explode(' ', $str_links);
$data['users']=$this->user->display_users();
$data['userdata'] = $this->user->userdata();
$data['customers']= $this->Customer_m->index();
// if(isset($_POST['submit'])){
$data["rsl"] =$this->CustomReport_m->search($username,$customer,$datefrom,$dateend,$config["per_page"],$page);
$this->load->view('customreport_v',$data);
// }else{
// redirect('CustomReport','refresh');
// }
}
This is my model
public function search_count($username,$customer,$datefrom,$dateend) {
$this->db->select('*');
$this->db->from('transactions');
if (!empty($customer)){
$this->db->where('t_customer_id',$customer);
}
if (!empty($username)){
$this->db->where('t_u_id',$username);
}
if (!empty($dateend)){
$this->db->where('t_date <=',$dateend);
}
if (!empty($datefrom)){
$this->db->where('t_date >=',$datefrom);
}
return $this->db->get()->num_rows();
}
public function search($username,$customer,$datefrom,$dateend,$limit,$start)
{
$start=$start-1;
$this->db->select('*');
$this->db->from('transactions');
if (!empty($customer)){
$this->db->where('t_customer_id',$customer);
}
if (!empty($username)){
$this->db->where('t_u_id',$username);
}
if (!empty($dateend)){
$this->db->where('t_date <=',$dateend);
}
if (!empty($datefrom)){
$this->db->where('t_date >=',$datefrom);
}
$this->db->limit($limit,$start);
$query=$this->db->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return $query->result();
}
}
this my route
$route['CustomReport/(:num)']="CustomReport/index/$1";

Search not working and fetch wrong results in Codeigniter

the Search doesn't work and retrieves wrong results
That worked with me if I searched with username only but when I made that search with username, customer and dates the results not become incorrectly
My Model
public function search_count($username,$customer,$datefrom,$dateend) {
$this->db->where('t_u_id',$username);
$this->db->where('t_customer_id',$customer);
$this->db->where('t_date <=',$dateend);
$this->db->where('t_date >=',$datefrom);
return $this->db->count_all("transactions");
}
public function search($username,$customer,$datefrom,$dateend,$limit,$start)
{
$start=$start-1;
$this->db->select('*');
$this->db->from('transactions');
$this->db->where('t_u_id',$username);
$this->db->where('t_customer_id',$customer);
$this->db->where('t_date <=',$dateend);
$this->db->where('t_date >=',$datefrom);
$this->db->limit($limit,$start);
$query=$this->db->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return $query->result();
}
}
My Controller
public function search($page_num = 1){
$this->load->library('pagination');
$this->load->helper("url");
$this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
$username=$this->input->post('select_user');
$username = ($this->uri->segment(3)) ? $this->uri->segment(3) : $username;
$customer=$this->input->post('select_customer');
$customer = ($this->uri->segment(4)) ? $this->uri->segment(4) : $customer;
$datefrom=$this->input->post('from');
$datefrom = ($this->uri->segment(5)) ? $this->uri->segment(5) : $datefrom;
$dateend=$this->input->post('to');
$dateend = ($this->uri->segment(6)) ? $this->uri->segment(6) : $dateend;
$total_row = $this->CustomReport_m->search_count($username,$customer,$datefrom,$dateend);
//$config['use_page_numbers'] = TRUE;
$config['base_url'] = site_url("CustomReport/search/$username/$customer/$datefrom/$dateend");
$config['total_rows'] = $total_row;
$config['per_page'] = 10;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
if ($this->uri->segment(7)) {
$page = ($this->uri->segment(7));
} else {
$page = 1;
}
$str_links = $this->pagination->create_links();
$data["links"]= explode(' ', $str_links);
$data['users']=$this->user->display_users();
$data['userdata'] = $this->user->userdata();
$data['customers']= $this->Customer_m->index();
if(isset($username)and !empty($username)){
$data["rsl"] =$this->CustomReport_m->search($username,$customer,$datefrom,$dateend,$config["per_page"],$page);
$this->load->view('customreport_v',$data);
}else{
redirect('CustomReport','refresh');
}
}
I think you should use this:
public function search_count($username,$customer,$datefrom,$dateend) {
$this->db->select('*');
$this->db->from('transactions');
$this->db->where('t_u_id', $username);
$this->db->where('t_customer_id', $customer);
$this->db->where('t_date <=', $dateend);
$this->db->where('t_date >=', $datefrom);
return $this->db->get()->num_rows();
}
Because count_all function just returns total of rows, it cannot apply where.

issue with pagination and limit

I have 3 tables site_settings,sermons & Preachers. In site_settings table, set the max number of preachers to be displayed. Preacher table contains all the preacher details and sermons table contains sermons of all preachers. Am using pagination for displaying preachers(2/page). if the value of site_settings table is 1 or 2 then no of preachers will display 2, if 3 or 4 then preachers 4... I don't know what is happening. Is it a problem with pagination or limit? I am using the following code for this ( In Model)
Method in Model
function viewAll($offset=0, $limit=null) {
$this->db->select('settings_value');
$this->db->from('site_settings');
$this->db->where('settings_name', 'preachercount');
$count = $this->db->get()->row();
echo $count->settings_value;
$preacher = array();
$this->db->select('preacher.preacher_id,preacher.first_name,preacher.last_name,preacher.preacher_image,preacher.preacher_logo,preacher.preacher_bio_brief');
$this->db->distinct('preacher.preacher_id');
$this->db->from('preacher');
$this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id');
$this->db->order_by('preacher.sort_order');
$this->db->limit($count->settings_value);
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_logo'] = $row->preacher_logo;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
return false;
}
Method in controller
function list() {
$paginate_segment = $this->uri->segment(3) ? $this->uri->segment(3) : 0;
$winner_list = $this->sermon_model->viewAllpreachers(0, null);
$config['base_url'] = base_url() . 'sermons/index/';
$config['total_rows'] = ($winner_list) ? count($winner_list) : 0;
$config['per_page'] = 2;
$config['full_tag_open'] = '';
$config['full_tag_close'] = '';
$config['uri_segment'] = 3;
$config['num_links'] = 4;
$config['display_pages'] = TRUE;
$config['first_link'] = 'First';
$config['first_link'] = 'First';
$config['first_tag_open'] = '<div>';
$config['first_tag_close'] = '</div>';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_close'] = ' | ';
$config['num_tag_close'] = ' | ';
//initialize pagination
$this->pagination->initialize($config);
$this->data['preachers'] = $this->sermon_model->viewAllpreachers($paginate_segment, $config['per_page']);
$this->data['links'] = $this->pagination->create_links();
$this->data['page'] = $this->config->item('APP_template_dir') . 'site/home/sermons_view';
$this->load->vars($this->data);
$this->load->view($this->_container);
}
As stated in the comments, you're using a limit twice and need to combine those methods:
function viewAll($offset=0, $limit=null) {
$this->db->select('settings_value');
$this->db->from('site_settings');
$this->db->where('settings_name', 'preachercount');
$count = $this->db->get()->row();
// now use value of [$count] if [$limit] is null - else use [$limit]
$limit = (is_null($limit)) ? $count : $limit ;
$preacher = array();
$this->db->select('...');
$this->db->distinct('preacher.preacher_id');
$this->db->from('preacher');
$this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id');
$this->db->order_by('preacher.sort_order');
// then remove the [limit()] call
// $this->db->limit($count->settings_value); <-- NOT NEEDED
$query = $this->db->get('', $limit, $offset);
This effectively allows you to pass a $limit to the method, or use the settings from the db.

Resources