Codeigniter从db获取所有行

Dar*_*ric 2 php mysql codeigniter

我正在使用任务管理器,其中有两种类型的用户:管理员(所有权限)和用户(有限权限).

这是控制器中的任务功能

function task($id) {
    $data = array(
        'query' => $this->main_model->get_task($id),
        'task_id' => $id,
        );
    $data2['comments'] = $this->main_model->get_comments($id);

    $this->load->library('form_validation');
    $this->load->helper('security');

    $this->form_validation->set_rules('comment_text', 'comment_text', 'trim|required|strip_tags|xss_clean');

    if ($this->form_validation->run() === FALSE)
    {
        if($this->main_model->get_task($id))
        {
            foreach ($this->main_model->get_task($id) as $tas)
            {
                $data = array(
                    'title' => $tas->task_name,
                    'desc' => $tas->task_description,
                    'date_created' => $tas->date,
                    'date_started' => $tas->task_start_date,
                    'deadline' => $tas->task_deadline,
                    'creator' => $tas->task_creator,
                    'owner' => $tas->task_owner, 
                    'attach'=>$tas->attachment_name,
                    'status'=>$tas->task_status,
                    'priority'=>$tas->task_priority,
                    'task_id'=>$tas->ID_task,
                    'base_url' => base_url()
                    );
            }
            $data1 = array(
                'emps' => $this->main_model->get_employees(),
                'creator' => $this->main_model->get_details($id),
                'owner' => $this->main_model->get_owner($id)
                );
             if ($this->session->userdata('employee_type') == 3) {

             $qu = $this->main_model->permission();
             $id = $this->session->userdata('id');
             $id_emp = $this->uri->segment(3);
             //foreach ($qu as $q) {
            if (in_array($id, $qu[0]) && in_array($id_emp, $qu[0])) {

                $this->load->view('task', array_merge($data, $data1, $data2));

             }
           else {
                echo "No Permission to access this task";
                }
            }
        }
    }
    else
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'doc|docx|pdf|txt|text|rtf|jpg|gif|png'; 
        $config['max_size'] = '1024';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {   
            if ("You did not select a file to upload." != $this->upload->display_errors('','')) {  
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('task', $error);
            }  
            else {  
                $this->main_model->set_comments($id);
                redirect(current_url());
            }  
        }  
        else {  
            $data = array('upload_data' => $this->upload->data());
            $data1 = $this->upload->data();
            $data2 = $data1['file_name'];
            $this->main_model->set_comments($id);
            $this->main_model->set_attachment($id, $data2);
            redirect(current_url());
        }  
    }
Run Code Online (Sandbox Code Playgroud)

}

和我在模型中的权限功能:

function permission() {
    $query = $this->db->get('employees_tasks');
    $ret = $query->result_array();


    return $ret;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试做的是,从数据库中获取所有数据,employee_tasks检查数据库中的ID_taskID_employee是否在同一行,但是我无法使其工作,我只获得第一行数据库,而不是其他数据库,那是我的key问题,从db获取所有行.我想查询数据库result,result_array,row,row_array,但由于某些原因,一切都在DB上市仅第一行.任何帮助,将不胜感激.

M K*_*aid 11

要从单独的table使用中获得所有结果,codeigniter Active record您可以这样做

function permission() {
   $this->db->select("*");
   $this->db->from("employees_tasks");
   $this->db->where('id', 'your id');
   $query = $this->db->get();        
   return $query->result();
   // OR
   $query = $this->db->query("SELECT * from employees_tasks WHERE your condition");
   return $query->result();
}
Run Code Online (Sandbox Code Playgroud)

希望它有意义

  • 你不需要使用`$ this-> db-> select("*");`或`$ this-> db-> from("employees_tasks");`!可以使用它:`$ query = $ this-> db-> get('employees_tasks');`.阅读:[Active Record](http://ellislab.com/codeigniter/user-guide/database/active_record.html) (6认同)