codeigniter pagination links doesnt work properly - codeigniter

I am using Codeigniter library pagination for my search.The problem is even if i have 2 results from the database table,clicking the next or previous link shows only one result.
code :
controller
function search()
{
$this->load->library('pagination');
$perpage =1;
$page = 1;
$search_content = $this->input->get('con_search');
$data->search = $this->public_model->search_content($search_content, $perpage);
$total = count($this->public_model->search_content($search_content));
$config['base_url'] = current_url()."?con_search=".$search_content;
$config['total_rows'] = $total;
$config['per_page'] = 1;
$config['cur_page'] = $page;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = "page";
$config['display_pages'] =TRUE;
$config['page_query_string'] = TRUE;
$config['cur_tag_open'] = '<span class="current">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<span class="prevtag">';
$config['prev_tag_close'] = '</span>';
$config['next_tag_open'] = '<span class="nexttag">';
$config['next_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data->footer = $this->public_model->get_footer();
$this->load->helper('text');
$this->load->view('header', $data);
$this->load->view('search');
$this->load->view('footer');
}
model:
function search_content($search_txt, $perpage = 0)
{
if($perpage){
$this->db->limit($perpage);
}
$this->db->select('title, description, alias');
$this->db->where('status', 'yes');
$this->db->like('description', $search_txt);
$res = $this->db->get('tbl_content')->result_array();
return $res;
}
view :
if($search && is_array($search)) {
foreach($search as $ind=>$val)
{
$this->db->select('menu_type_id');
$this->db->where('menu_alias', $val['alias']);
$res = $this->db->get('tbl_menu')->row_array();
if($res) {
if($res['menu_type_id'] == 2)
$url = site_url('page/'.$val['alias']);
else
$url = site_url('page/footer/'.$val['alias']);
}
?>
<div class="box_1">
<div class="deal_contents">
<div class="bx_0"><?php echo $val['title'] ?></div>
<div class="bx_1" style="width:670px;"><?php echo strip_tags(character_limiter($val['description'],200)) ?></div>
<div class="bx_2" style="text-align:right; width:640px;">Read More</div>
</div>
</div>
<?php
}
echo $this->pagination->create_links();
}
thanks in advance

Do like this
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();

Related

How to show continued numbered of data with pagination

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

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.

code igniter pagination for displaying specific data from database?

I want to display some specific data from database through pagination using CI.but pagination by default passess parameter in function which creates problem for me.
Here is my Controller
function page($catagoryid){
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/pagination/page/';
$count = $this->db->query("select * from tbl_products where category_id='$categoryid'");
$total=$count->num_rows();
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->model("mpagination");
$data['list'] = $this->mpagination->get_s(,$categoryid,$config['per_page'],$this->uri->segment(3));
if ($data['list']!== null){
$this->load->view('pagination_view', $data);
}
else {
$this->load->view('noresult');
}
}
Here is my Model
function get_s($categoryid,$num, $offset) {
$this->db->select ('*');
->where('category_id',$categoryid); // field name
$sql = $this->db->get('tbl_products',$num, $offset); // table name
if ($sql->num_rows () >0) {
return $sql->result();
}
else {
return null;
}
}
Here is my view
<?php
foreach($list as $row):?>
<?php echo $row->_prduct_id." ".$row->name." ".$row->description;?>
<br/>
<?php endforeach;?>
<br/>
<?php echo $pagination; ?>
This code work at first but when i clicked on second link of pagination it will not work because by default pagination sends its value to url as parameter and doesnot show data.
what can i do?
I didn't understand your code properly but this is the way you need to do pagination.
public function page($category_id)
{
$result = $this->model_file->model_function($category_id);
$start_index = 0;
$total_rows = count($result);
$items_per_page = 10;
$filtered = array_splice($result,$start_index,$items_per_page);
$model['data'] = $filtered;
$model['page_link'] = create_page_links (base_url()."/controller_file_name/page", $items_per_page, $total_rows);
$this->load->view('view_file_name_path',$model);
}
function create_page_links($base_url, $per_page, $total_rows) {
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
Try like this
public function index($offset = 0)
{
$language_id = 1;
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();
}

Resources