CodeIgniter - 在非对象上调用成员函数num_rows()

时间:2013-08-06 01:00:43

标签: codeigniter pagination fatal-error

我目前刚接触CodeIgniter并尝试使用codeigniter中的分页类添加分页。

控制器:

public function view_emp(){
    $data['design'] = 'emp_view';
    $this->load->model('user_model');
    $data = $this->user_model->getall();
    $total_rows = $data->num_rows();

    $config['base_url'] = base_url() . 'main';
    $config['total_rows'] = $total_rows;
    $config['per_page'] = 10;

    $this->pagination->initialize($config); 
    $this->view_data['pagination'] = $this->pagination->create_links();
    $this->view_data['result'] = $data;

    if($this->session->userdata('is_logged_in')){
        $this->load->view('includes/template', $data);
    }
    else{
        redirect('main/restricted');
    }

型号:

public function getall(){
    $query = $this->db->get('another_user');
    return $query->result();
}
public function getall_limit($start_row, $limit){
    $data = "SELECT * FROM another_user limit $start_row, $limit";
    $result = $this->db->query($data);
    return $result;
}

在我看来,我只是从我的控制器回应了分页 这里有人能帮帮我吗?我现在真的很难过。

2 个答案:

答案 0 :(得分:0)

我认为问题出在模型层,而不是视图层。听起来你的查询失败了。在$ query-> result();

上执行var_dump()或print_r()

答案 1 :(得分:0)

试试这个,你的模型的变化:

 function getall(){
    return $this->db->count_all_results('another_user');
 }

 function getall_limit($limit = 0){
    $data   = array();
    $rs = $this->db->get('another_user', 10, $limit);
    if($rs->num_rows() > 0){
        $data   = $rs->result();
    }
    $rs->free_result();
    return $data;
 }

控制器

中的更改
 function view_emp(){
    $data['design'] = 'emp_view';
    $this->load->model('user_model');
    $data = $this->user_model->getall();
    $total_rows = $data['rows'];

    #pagination start
    $this->load->library('pagination');
    $config['base_url']         = base_url() . 'main';
    $config['total_rows']       = $this->user_model->getall();
    $config['per_page']         = 10;
    $config["uri_segment"]      = 3;
    //$config['next_link']        = 'Next';
    //$config['prev_link']        = 'Prev';
    //$config['cur_tag_open']     = '<span class="active_page">';
   // $config['cur_tag_close']    = '</span>';
    $this->pagination->initialize($config);
    #pagination end



    $this->pagination->initialize($config); 
    //$this->view_data['pagination'] = $this->pagination->create_links();
    $this->view_data['result'] = $this->user_model->getall_limit($this->uri->segment(3));

    if($this->session->userdata('is_logged_in')){
        $this->load->view('includes/template', $data);
    }
    else{
        redirect('main/restricted');
    }
}