我的分页不起作用

时间:2015-09-18 07:23:53

标签: codeigniter pagination

为什么我的分页不适用于其他正常工作的功能。它显示了分页的链接,但第一页显示了所有数据,该页面的另一个链接不显示数据。

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

此功能计数数据库

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

1 个答案:

答案 0 :(得分:1)

尝试这个:

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

在您的型号代码中:

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

}