My view not separate data by use pagination - codeigniter

I use pagination for separate data in the view page it's show the links for select page but it's show all data in database (250 data) it's not separate 30 per page. Please Help me
This my model
function get_item_code($param){
$result = array();
$sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt ";
$sql .= "from tbl_item_code ic ";
$sql .= "left join ( tbl_lines_code lc inner join ( select id, max(dt) maxdt from tbl_lines_code where active = 1 group by id ) maxdt ";
$sql .= "on lc.id = maxdt.id and lc.dt = maxdt.maxdt ) on ic.id = lc.id ";
$sql .= "where ic.active = 1 group by ic.id ";
// echo $sql;
if ($param->limit > 0)
$this->db->limit($param->per_page, $param->limit);
else
$this->db->limit($param->per_page);
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$result = $query->result();
}
return $result;
}
This my controller
public function main(){
$this->load->library('pagination');
$page = $this->uri->segment(3);
$param = new stdClass();
if (!isset($page) || $page == '') {
$page = 1;
}
$param->per_page = 30;
$param->limit = ($page - 1) * $param->per_page;
$paginate_url = site_url('warehouse/main');
$data['total_result'] = $this->m_stock->count_stock ();
$config['uri_segment'] = 3;
$config['num_links'] = 4;
$config['base_url'] = $paginate_url;
$config['total_rows'] = $data['total_result'];
$config['per_page'] = $param->per_page;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = FALSE;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['item'] = $this->m_stock->get_item_code($param);
$this->load->view('v_all_stocks', $data);
}
and this my view foe echo $pagination
<?php
if(isset($pagination) && $pagination != ''){
?>
<div class="search_pagination"><?php echo $pagination; ?></div>
<?php } ?>
and use foreach for show data

In controller
function main()
{
$this->load->library('pagination');
$count = $this->m_stock->count_stock();
$config['base_url'] = base_url() . 'warehouse/main/';
$config['total_rows'] = $count;
$config['per_page'] = 30;
$config['uri_segment'] = 3;
$limit = $config['per_page'];
// Bootstrap style
$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);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();
$data['item'] = $this->m_stock->get_item_code($limit,$page);//pass item code here
$this->load->view('v_all_stocks', $data);
}
In model
function get_item_code($limit,$page)
{
$sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt ";
$sql .= "from tbl_item_code ic ";
$sql .= "left join ( tbl_lines_code lc inner join ( select id, max(dt) maxdt from tbl_lines_code where active = 1 group by id ) maxdt ";
$sql .= "on lc.id = maxdt.id and lc.dt = maxdt.maxdt ) on ic.id = lc.id ";
$sql .= "where ic.active = 1 group by ic.id ";
$sql .= "LIMIT $page, $limit";
$query = $this->db->query($sql);
$result = $query->result_array();
return $result;
}

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

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.

Pagination on Codeigniter

//Controller
function search($search = 0)
{
$this->form_validation->set_rules("search", "Search", "trim|required|xss_clean");
$searchemp = $this->input->post('search');
$data['result'] = $this->main_model->search_employee($searchemp);
$data['resultcount'] = count($this->main_model->search_employee($searchemp));
//$result = $this->main_model->search_employee($searchemp);
//$this->display($this->main_model->search_employee($searchemp));
$config['base_url'] = base_url().'main/search';
$config['anchor_class'] = 'test';
$config['total_rows'] = $data['resultcount'];
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_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>';
$config['next_link'] = '»';
$this->pagination->initialize($config);
$tmpl = array ( 'table_open' => '<table class="table table-bordered table-hover">' );
$this->table->set_template($tmpl);
//$data['searchresult']=$this->db->get($result, $config['per_page'],$search);// take record of the table
$header = array('Employee Number','Name'); // create table header
$this->table->set_heading($header);// apply a heading with a header that was created
$this->load->view('results_view',$data);
}
//Model
function search_employee($searchemployee)
{
$sql = "select Empcode, Name from spmm_employee_info where name like '%" . $searchemployee . "%'";
$query = $this->db->query($sql);
return $query->result_array();
}
When i try to search and click on the other page, it displays all the data on the table spmm_employee_info. All i want is to display all the results related to the search and not all data when i click the page on the pagination. Please help
Use mysql DATE_FORMAT and convert date time in time Like this demo query
SELECT DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY from tbl where TIMEONLY='7:01AM'

codeigniter pagination links doesnt work properly

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

Resources