Search for more data after finding nothing related to ID

Asked

Viewed 44 times

0

I am making a 4x4 matrix system for a client and I 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 search in the next indicated to the side, until the end. If there is no return a message talking that has a vacancy available.

See the photo: http://i.imgur.com/hfLU8bp.png

The system checks at level 1: As seen in the photo has 4 The system then checks on the first user (Bruno) if below it (level 2) has 4, if it has not returned a message talking that has vacancy. In the photo has 4 below Bruno, then it would be right for him to check in João Vitor (on the side), but instead the system looks 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 top 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 entire second level) and what it needed to return me was Nivel 2 disponivel na ID 7

In short: He has to check at the current level if in one of the members who are at that level there are 4 people, if there are, go to the next member, until the end. If all return that has 4 or more, then descend one more level and do the same check.

  • Are you selecting the 2 level of the same user? why when you enter the loop of a user who already has four below it this code is going to the first of these 4 below Bruno

2 answers

0

I made an adaptation of your code, because I don’t have the database, I think it would be something like, 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 );


            }    
    }

$usuarios = array( 'Bruno' =>array( 'Júlia', 'Geremias', 'Leo', 'Feranda'), 'João Victor', 'Vinicius', 'Andressa' );

Quantitylinematrix( $usuarios, 1 );

  • I filled out the other names (João Victor, Vinicius e Andressa) as if they had 4 also like Bruno and he shows me nothing. The right thing would then he look inside Júlia now, because he did not find in any of the others (first line), then he returns and looks inside the Bruno First. understand ? rest ta OK your code

0

Well I didn’t test this code because I don’t have a way, maybe I need to make adjustments, but the idea is to loop outside with the levels allowed 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++; 
}

Browser other questions tagged

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