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:
I need to list this return saying the name of the employee and the skills he has
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.
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.
"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?
– Woss
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.
– Lucas
You can use an aggregation function in the skills by grouping the columns in common and treat the value in PHP for display.
– Woss
But will your screen display 1000 (or 1 million) users at once? Why?
– Woss
Which function should I search to find explanations about?
– Lucas
1000 no, but it’s close to 700 employees, although I’ll pay to show up 10 or 20 at a time
– Lucas
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 haveforeach
insideforeach
.– Woss
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.
– Lucas