【问题标题】:CodeIgniter - Call to a member function num_rows() on a non-objectCodeIgniter - 在非对象上调用成员函数 num_rows()
【发布时间】:2013-08-06 20:54:39
【问题描述】:

我目前是 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;
}

在我看来,我只是从我的控制器呼应了 $pagination 这里有人可以帮助我吗?我现在真的很难过。

【问题讨论】:

  • 您需要将$this->view_data 作为$this->load->view() 的参数传递,而不是$data
  • 喜欢这个? $this->load->view('includes/template', $this->view_data??
  • 您是否尝试过调试控制器中的变量?尝试呼应它们。 echo $this->view_data['pagination']; die(); 看看你是否有任何意见。另外,不工作是什么意思?请详细描述问题,是否有错误?
  • 是的,错误是在非对象上调用成员函数 num_rows(),这是错误。
  • 错误似乎在 num_rows() 部分。

标签: codeigniter pagination fatal-error


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个,改变你的模型

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2011-12-23
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      相关资源
      最近更新 更多