Foreach in Inner Join

Asked

Viewed 57 times

0

Good afternoon, I have an sql query with Inner Join.

  $this->db->select('f.nome, f.sobrenome, h.habilidade');
  $this->db->from('funcionarios as f');
  $this->db->join('habilidades as h', 'f.id = h.id_funcionario');
  $query = $this->db->get();
  return $query->result();

The sql return is:

inserir a descrição da imagem aqui

I need to list this return saying the name of the employee and the skills he has

inserir a descrição da imagem aqui

But if I take and give a foreach like this:

foreach ($freelancers as $row){ 
  echo $row->nome; echo $row->sobrenome;
  echo $row->habilidade;
}

Then he picks up and prints the same person over and over again, which is natural. inserir a descrição da imagem aqui

My question is, what’s the best way to do that?

Note: I want to do in a way that is not to make 2 selects and use foreach inside of foreach with if.

  • 1

    "I want to do in a way that is not to make 2 selects and use foreach inside of foreach with if" why not these solutions?

  • Imagine that you have 10 employees and each one of them has 20 skills (although in the proposal will be presented only 4 skills). Ai you do a foreach on the employees, and then a foreach within the skills. That would be: 10 x 20 = 200 reps, that doesn’t make much difference in processing Now imagine it’s 1000 employees. 1000 x 20 = 20,000 repetitions instead of 1000 of them, the processing ends up being toasted each time the system receives new data. Now imagine that any system has 1 million users with 20 skills.

  • You can use an aggregation function in the skills by grouping the columns in common and treat the value in PHP for display.

  • But will your screen display 1000 (or 1 million) users at once? Why?

  • Which function should I search to find explanations about?

  • 1000 no, but it’s close to 700 employees, although I’ll pay to show up 10 or 20 at a time

  • Exactly. So you don’t have to worry about the case of processing 1000 at a time when it will display only 10 or 20. It’s wanting to solve a problem you don’t have. But you can search for group_concat. Spolier: you will have foreach inside foreach.

  • Uhn, I will search about group_concat to understand about and I will test foreach with foreach on the page and see how it will look, thanks for your attention.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.