swett.bp
swett.bp

Reputation: 313

My pagination is not working

Why my pagination is not working in another function it's working. It's show the link of pagination but the first page show all data another link of the page it's not show data.

public function index(){
    $this->load->library('pagination');
    $page = $this->uri->segment(2);
    $param = new stdClass();
    if (!isset($page) || $page == '') {
        $page = 1;
    }
    $param->per_page = 5;
    $param->limit = ($page - 1) * $param->per_page;

    $paginate_url = site_url('warehouse');

    $data['total_result'] = $this->m_stock->count ($param);

    $config['uri_segment'] = 2;
    $config['num_links'] = 3;
    $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);
}

This function count database

function count(){
  $this->db->select("*");
  $this->db->from("tbl_item_code");
  $this->db->where('active', '1');
  $num_results = $this->db->count_all_results();
  return $num_results;
}

Upvotes: 1

Views: 155

Answers (1)

Mohan
Mohan

Reputation: 4829

TRY this :

public function index(){
    $this->load->library('pagination');
    $page = $this->uri->segment(2);
    $param = new stdClass();
    if (!isset($page) || $page == '') {
        $page = 1;
    }
    $param->per_page = 5;
    $param->limit = ($page - 1) * $param->per_page;

    $paginate_url = site_url('warehouse');
    $results = $this->m_stock->get_item_code($param);  //get the db query object here
    $data['total_result'] =  $results->num_rows(); //get the number of results for this query

    $config['uri_segment'] = 2;
    $config['use_page_numbers'] = TRUE;  //add this line if you wnat to use page numbers instead of offset in url
    $config['num_links'] = 3;
    $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'] = $results->result();  //get the results
    $this->load->view('v_all_stocks', $data);
}

In Your Model Code :

function get_item_code($param){
    $result = array();
    $sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance ";
    $sql .= "from tbl_item_code ic ";
    $sql .= "left join ( tbl_lines_code lc inner join ( select id, max(dt) maxdt from tbl_lines_code 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";
    if ($param->limit > 0)
        $this->db->limit($param->per_page, $param->limit);
    else 
        $this->db->limit($param->per_page);
     return $this->db->query($sql);  //change it to return the db query object

}

Upvotes: 1

Related Questions