With "echo" it works and with "Return" it doesn’t

Asked

Viewed 328 times

-2

I have a function, and it checks if the ID that I’ve informed has more than six records in the database. If it has, then it takes, makes a select to return these 6 records and plays in an array and executes the same function, ie recursive function. If you have less than 6 records, then the system returns that same informed ID.

The problem is this, when I give "Return" he shows me "null" and when I use "echo" in place of "Return" it returns me the ID as should be the functioning of the code.

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

I don’t want to return everyone who is less than 6... When I call function for the first time, pass 1 ID only, then if it is less than 6 returns only it. But if you have 6 or more, then I put them all in an array to do the same check. If in the first contents of the array return less than 6 so you don’t need to check the rest of the content array. Actually I only need the first 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);
    }
}
  • 1

    because uses Return if echo that works????

  • put it like this echo $this->usuario_model->EscolhePatrocinadorRede(array(55));

  • This method is inside a controller in Codeigniter???

  • @Not virgilionovic, it’s inside a model

2 answers

2

Every function should do or return something, it seems obvious, but that’s exactly where you’re getting confused.

When you call the function so:

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

and inside of her you have:

...
return $IDPatrocinador;
...

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

<?php
123;
?>

i.e., it is doing absolutely nothing with the value returned by the function.

Now, if you call the function so:

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

will be the same as:

<?php
echo 123;
?>

soon, will print the value as desired.

  • I did the echo like you said echo $this->usuario_model->Choose sponsor(array(55)); and returns nothing if I use Return

  • Probably your first query is returning more than 2 records, so below you call the function itself only that is doing nothing with the return... Add a Return in front of that second function call...

  • 1

    In fact its function is more complicated than it should be, and with the possibility of looping, I suggest you do an optimization in the structure and make it impossible to loop it.

1


Following the reasoning of my previous answer:

As you are calling the function within the function itself, you need to do something with the result of this second call, ie return to where you first called it:

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);
    }
}
  • Thank you!. That’s right. So simple I didn’t see. Thank you very much Jader!

Browser other questions tagged

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