Welcome Guest, Not a member yet? Register   Sign In
Passing variable to view results in empty variable
#1

OK, I am at my wit's end with this one. I have googled and tried everything I can think of. It is possibly very easy to fix, but I am unable to do so. The problem lies in the family part of the controller. Basically I want to fetch the familyId from the session and find all other members of the family from the database. The query works, but sending the data to the view does not. 

I have a controller. Focus on the family-part:
Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\UserModel;

class Dashboard extends BaseController
{
    protected $session;

    function __construct()
    {
        $this->session = \Config\Services::session();
    }

    public function index()
    {
        return view('dashboard');
    }
    public function family()
    {

        $userModel = new UserModel();
        $familyId = $this->session->get("familyId");
        $data = $userModel->where('familyId', $familyId)->findAll();
        return view('family', $data);
//        print_r ($data);
    }
    public function tasks()
    {

    }
    public function goals()
    {

    }

}

A simple view:
Code:
<?php

if (empty($data))
{
    echo 'Empty';
} else {

    print_r($data);
}

?>



If I print_r($data) from the controller I get an array:
Code:
Array ( [0] => Array ( [id] => 1 [email] => [email protected] [password] => $hash [created_at] => [updated_at] => [familyId] => 1 [givenName] => Andy [familyName] => H [userName] => Desde [child] => 0 ) )

But if I pass the exact same variable I get nothing coming out in the view.

Can you see what is wrong with my code?
Reply


Messages In This Thread
Passing variable to view results in empty variable - by Delph1 - 03-06-2023, 12:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB