Find more data after finding nothing related to ID

0

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.

    
asked by anonymous 09.04.2016 / 08:18

2 answers

0

I made an adaptation of your code, because I do not have the database, I think it would look something like that, of course I'm not passing the level as a parameter in the function.

      function QuantidadeLinhasMatriz( $usuarios, $contador  ){
            $tempArray = array();
            foreach( $usuarios as $k => $v ){

                       if ( is_array( $v ) and count($v) == 4 )
                        {  
                            $contador = 1;
                            echo "  \$usuarios[$k] => $v NAO TEM VAGA.\n   |"; 
                            continue;   
                        }

                       $tempArray =  $v;
                       if ( count( $k ) < 4  ){
                           echo "$v TEM VAGA     |"; 
                           $contador = 1;
                           continue;
                        }

                      echo "  $usuarios[$k] => $v.\n";
                      $contador += 1; 

                       QuantidadeLinhasMatriz( $tempArray, $contador );


            }    
    }

$ users = array ('Bruno' => array ('Julia', 'Geremias', 'Leo', 'Feranda'), 'John Victor', 'Vinicius', 'Andressa'); >

QuantityLinesMatrix ($ users, 1);

    
09.04.2016 / 22:13
0

Well I did not test this code because I do not know how, maybe I need to make adjustments, but the idea is to loop out with the allowed levels for each iteration.

$nivelInicial   = getNivelMinimo(Patrocinador); // TO-Do
$nivelMaximo    = getNivelMaximo(Patrocinador); // TO-Do
$nivelPermitido = $nivelInicial +1;
$contador = $nivelInicial;

 public function QuantidadeLinhasMatriz($id, $nivel = 1, $permitido ){

            $this->db->where('id_patrocinador', $id);
            $patrocinadores = $this->db->get('patrocinadores');

            if($patrocinadores->num_rows() >= 4){

                foreach($patrocinadores->result() as $row){

                    if ( $nivel > $permitido )
                       continue;

                    echo $this->QuantidadeLinhasMatriz($row->id_usuario, $nivel+1 ).'<br />';
            }

            }else{
                return 'Nivel '.$nivel.' disponivel na ID '.$id;
            }
    }

 while ( $contador  <= $nivelMaximo ){
        QuantidadeLinhasMatriz( $id_patrocinador, $nivelInicial, $nivelPermitido );
        $nivelInicial += 2;
        $contador += 2;
        $nivelPermitido++; 
}
    
10.04.2016 / 12:55