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.
}of thefor-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.