-1

I'm accessing the database to retrieve a list of employee ids and then take that array of employee id to run a query against another table on the database that gets all the information of the person based off the employee id but I'm only getting the last value in the statement but I need to get and loop for all of them. Can anyone see my error?

public function manager_list()
{
    $results = array();
    $managers = $this->db->query('select `employee_id` from `managers`');
    
    foreach($managers->result() as $row)
    {
        $employee_id = $row->employee_id;
    }
    
        $query = $this->db->query('select `employee_id`, `ssn`, `first_name`, `last_name`, `department`, `title`, `status` from `employees` where `employee_id` = "'.$employee_id.'"');
    
        foreach ($query->result() as $row){
            $results[] = array(
            'employee_id' => $row->employee_id,
            'ssn' => $row->ssn,
            'first_name' => $row->first_name,
            'last_name' =>$row->last_name,
            'department' =>$row->department,
            'title' =>$row->title,
            'status' =>$row->status,
        );
        }
    return $results; 
}

This is using the code igniter framework.

7
  • You need to move the closing brace after getting the employee ID to right above the return. The second query (and loop) needs to be within the braces for the first loop. Commented May 14, 2014 at 20:53
  • 1
    1. The closing } of the for-loop is misplaced. 2. Surely this could be done better with a single SQL query using a JOIN rather than a whole bunch of queries in a loop. Commented May 14, 2014 at 20:55
  • 1
    Why not do a JOIN in the query? Do you have a need to run two queries? Commented May 14, 2014 at 20:56
  • 1
    There is really no need for two queries here unless you're using the employee_id's somewhere else that you've not told us about. Commented May 14, 2014 at 21:09
  • 1
    Relevant reading: SELECT WHERE IN subquery using CodeIgniter's where_in() method and Using a subquery with CodeIgniter WHERE IN clause Commented Jul 26 at 4:57

2 Answers 2

2

Here is a short version with JOIN (replaces your whole code):

public function manager_list()
{
    return $this->db
    ->select('e.employee_id, e.ssn, e.first_name, 
              e.last_name, e.department, e.title, e.status')
    ->join('managers AS m', 'e.employee_id = m.employee_id')
    ->where(array('e.employee_id' => $employee_id))
    ->get('employees AS e')
    ->result_array();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Do one query with a JOIN and do one loop -

public function manager_list()
{
    $results = array();
    $managers = $this->db->query('SELECT `m`.`employee_id`, `e`.`ssn`, `e`.`first_name`, `e`.`last_name`, `e`.`department`, `e`.`title`, `e`.`status` FROM `managers` AS `m` JOIN `employees` AS `e` ON `m`.`employee_id` = `e`.`employee_id`');

    foreach ($managers->result() as $row){
        $results[] = array(
            'employee_id' => $row->employee_id,
            'ssn' => $row->ssn,
            'first_name' => $row->first_name,
            'last_name' =>$row->last_name,
            'department' =>$row->department,
            'title' =>$row->title,
            'status' =>$row->status,
        );
    }
    return $results;
}

3 Comments

I believe the whole foreach part is slightly redundant because it does exactly what the native CI method result_array() does, right?
Yes, it is redundant. I was just making enough change in the OP's code for the point to be made about the JOIN. BTW +1 for your excellent elegant solution @Shomz
Thank you @Shomz and Jay for telling me the foreach is redundant. I was unaware it was because the first time I looked up how to return an array with code igniter was with a foreach. You have saved me some time, and code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.