I am doing a 4x4 array system for a client and am having a difficulty in the following question: The system has to check if the indicated to be checked already has 4 people in the network, if there is then look at the next indicated next, until the end. If there is no return a message saying that there is a vacancy available.
See the photo: link
The system checks at level 1: As seen in the picture it has 4 The system then checks on the first user ( Bruno ) if below it (level 2) has 4, if it does not return a message that has a vacancy. In the photo you have 4 underneath Bruno , then the right one would be to check it on João Vitor (next to it), but instead, the system searches below Julia which is indicated by Bruno .
I have the code
public function QuantidadeLinhasMatriz($id, $nivel = 1){
$this->db->where('id_patrocinador', $id);
$patrocinadores = $this->db->get('patrocinadores');
if($patrocinadores->num_rows() >= 4){
foreach($patrocinadores->result() as $row){
echo $this->QuantidadeLinhasMatriz($row->id_usuario, $nivel+1).'<br />';
}
}else{
return 'Nivel '.$nivel.' disponivel na ID '.$id;
}
}
As can be seen on the upper left side of the print, it is printed:
Nivel 3 disponivel na ID 9
Nivel 3 disponivel na ID 10
Nivel 3 disponivel na ID 11
Nivel 3 disponivel na ID 12
Nivel 2 disponivel na ID 7
Nivel 2 disponivel na ID 8
Nivel 2 disponivel na ID 17
The script until it is running correctly, but it is already checking the third level (without finishing checking the whole second level) and what it needed to only return me was Nivel 2 disponivel na ID 7
In short: He has to check at the current level if one of the members that is at this level has 4 people, if he has, he goes to the next member until the end. If all return that has 4 or more, then go down one level and do the same verification.