With "echo" works and with "return" no

-2

I have a function and it checks if the given ID has more than 6 records in the database. If it does, then it picks up, makes a select to return those 6 records and plays in an array and performs the same function, that is, recursive function. If you have less than 6 records, then the system returns this same ID.

The problem is this, when I give return it shows me " null" and when I use echo instead of "return" It returns me the ID how the code should work.

I'm calling this first: $IDPatrocinador = $this->usuario_model->EscolhePatrocinadorRede(array(55))

  

I do not want to return everyone who is less than 6 ... When I call   for the first time, it passes 1 ID only, so if it is less than 6   returns only her. But if I have 6 or more then I put all of them in an array   to do the same verification. If in the first array contents   return less than 6 so you do not need to check the rest of the content   of the array. I actually need only the first one to give less than 6.

public function EscolhePatrocinadorRede($id_patrocinador){

    if(!empty($id_patrocinador)){

        foreach($id_patrocinador as $IDPatrocinador){

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

            if($patrocinadores->num_rows() < 3){

                return $IDPatrocinador;
            }
        }

        $idUsuario = array();

        foreach($id_patrocinador as $IDPatrocinador){

            $this->db->order_by('id_usuario', 'ASC');
            $this->db->where('id_patrocinador', $IDPatrocinador);
            $patrocinadores = $this->db->get('patrocinadores');

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

                $idUsuario[] = $patrocinador->id_usuario;

            }
        }

        $this->EscolhePatrocinadorRede($idUsuario);
    }
}
    
asked by anonymous 15.10.2016 / 07:04

2 answers

1

Following the reasoning of my previous answer:

As you are calling the function inside the function itself, you have to do something with the result of that second call, or return to where you called it for the first time:

public function EscolhePatrocinadorRede($id_patrocinador){

    if(!empty($id_patrocinador)){

        foreach($id_patrocinador as $IDPatrocinador){

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

            if($patrocinadores->num_rows() < 3){

                return $IDPatrocinador;
            }
        }

        $idUsuario = array();

        foreach($id_patrocinador as $IDPatrocinador){

            $this->db->order_by('id_usuario', 'ASC');
            $this->db->where('id_patrocinador', $IDPatrocinador);
            $patrocinadores = $this->db->get('patrocinadores');

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

                $idUsuario[] = $patrocinador->id_usuario;

            }
        }
        // estava faltando esse return para mandar o resultado para onde ocorreu a primeira chamada da função
        return $this->EscolhePatrocinadorRede($idUsuario);
    }
}
    
24.10.2016 / 20:20
2

Every function should do or return something, this seems obvious, but it is exactly where you are getting confused.

When you call the function like this:

<?php    
$this->usuario_model->EscolhePatrocinadorRede(array(55));
?>

And inside it you have:

...
return $IDPatrocinador;
...

Its function is to assign a value to $IDPatrocinador and then return that value to the scope that called it, so if it returns the value 123 for example, it is the same as:

<?php
123;
?>

that is, you are doing absolutely nothing with the value returned by the function.

Now, if you call the function like this:

<?php    
echo $this->usuario_model->EscolhePatrocinadorRede(array(55));
?>

will be the same as:

<?php
echo 123;
?>

logo will print the value as desired.

    
15.10.2016 / 11:48