0

我目前是 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 有人可以帮我吗?我现在真的很难过。

4

2 回答 2

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');
    }
}
于 2013-08-06T06:25:23.740 回答
0

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

于 2013-08-06T03:33:03.883 回答