Dario Juric
Dario Juric

Reputation: 21

Codeigniter get all rows from db

I'm working on task manager where have 2 types of user: Administrators(all privileges), and Users(limited privileges).

Here's my task function in controller

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

}

and my permission function in model:

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


    return $ret;
}

What I'm trying to do is, get all data from database employee_tasks check if the ID_task and ID_employee are in the same row in database, but I can't get it working, I only getting first row of database, not the others, and that's my key problem, get all rows from db. I tried query database with result, result_array, row, row_array, but for some reason everything is listing only first row in db. Any Help would be appreciated.

Upvotes: 2

Views: 49883

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

To get all results from a separate table using codeigniter Active record you can do like this

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

Hope it makes sense

Upvotes: 10

Related Questions