Welcome Guest, Not a member yet? Register   Sign In
Active records - loop within a loop necessary?
#1

[eluser]Unknown[/eluser]
I am working with 2 tables -- jobs & users. I've successfully retrieved all fields from the jobs table, but I would also like to include the username with each job. For that I have to reach into the users table.

In my jobs table I have a users_id field which matches the id field of my users table. Am I going about this the right way?

Code:
function get_jobs($limit, $start) {
  $this->db->limit($limit, $start);
  $query=$this->db->get('jobs');
  if($query->num_rows()>0) {
   foreach($query->result() as $row) {
    
    $this->db->where('id', $row->user_id);
    $usernames=$this->db->get('users');
    foreach($usernames->result() as $username) {
     $data[]=$username->username;
    }
    
    $data[]=$row;
   }
   return $data;
  }
  return NULL;
}




Theme © iAndrew 2016 - Forum software by © MyBB