0

hi I'm a kind of a newbie to the MVC in programming. I tried to findout the solution from stackoverflow and other sites by googling but none of are worked for me.

Please guide me where i'm doing mistake.

My controller code:

function getAllusers($id){
    global $conn;
    $users = [];
    $sql = "SELECT * from Users WHERE id=$id";
    $ownerdata = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($ownerdata))
    {
        $users[] = $row;
    }
    return $users;
}

View (piece of) code:

    getAllusers($user_id);
    var_dump($users);

In var_dump($users); I'm getting null and in controller if I do var_dump() before return I'm getting an array.

Please, guide me to overcome this. Thank you!

1
  • Only a return statement does return something (or the new keyword for creating an object instance). The while loop can process all variables in its outer and inner scope. Commented May 23, 2020 at 18:46

2 Answers 2

1

Your function getAllusers() returns the value so, you need to put this value into a variable to use inside your view.

You have two files:

Controller: That communicates with your data (database, etc.) View: Where you display your data that you collected from some controller call.

You can see more on MVC patterns in how you organize a structure to create systems.

What happens when you created a function getAllusers()? The function get the data on database and create the array as showed on your question.

When you call the function getAllusers() your $users variable are on the scope of function. This means that $users variable are acessible only inside the function getAllusers(). See more details here.

As you call getAllusers() in another file, when the function ends, the $users inside function are erased from memory. So, the return function is to get the piece of data that you need and get back.

So, when you create a new variable with the same name outside the function like showed on the code below, this new variable simply receives the return from the function called and you don't lose the result of your work.

You also can do the same thing with other variable name when you receive the value.

Try this:

   $users = getAllusers($user_id);

This will put the return from getAllusers() function on variable $users.

Sign up to request clarification or add additional context in comments.

2 Comments

hi thank you very much @william it worked me as charm. if u dont mind can u please give me a lil explanation about this.
thank you very much for your guidance this made me clear concept about MVC. it's really helpful thank you.
0

Just one tiny thing:

$users = getAllusers($user_id);
var_dump($users);

3 Comments

hi @adam winter it worked as charm. if u dont mind can u please tell me what actually happening here with this functions.
Same as William said. getAllusers() gives $users a value. Without doing that, $users has no value. var_dump() has no knowledge of what happened INSIDE of getAllUsers(). The result has to come out of the function and be given to something that it has knowleddge of.
yes thank you very much @adam winter. this helps me lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.