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
#2

See https://codeigniter4.github.io/CodeIgnit...o-the-view
Reply
#3

(This post was last modified: 03-06-2023, 10:23 PM by HermyC.)

Try this..
PHP Code:
public function family()
    {
        $userModel = new UserModel();
        $familyId $this->session->get("familyId");
        $familydata $userModel->where('familyId'$familyId)->findAll();
 
$data = ['familydata' => $familydata];
        return view('family'$data);
//        print_r ($data);
    
and for the view..
PHP Code:
if (empty($familydata))
{
    echo 'Empty';
} else {

    print_r($familydata);

Reply
#4
Lightbulb 

(03-06-2023, 10:21 PM)HermyC Wrote: Try this..
PHP Code:
public function family()
    {
        $userModel = new UserModel();
        $familyId $this->session->get("familyId");
        $familydata $userModel->where('familyId'$familyId)->findAll();
 
$data = ['familydata' => $familydata];
        return view('family'$data);
//        print_r ($data);
    
and for the view..
PHP Code:
if (empty($familydata))
{
    echo 'Empty';
} else {

    print_r($familydata);

THANK YOU! I understand what I did wrong now. Big props to you!
Reply
#5

It sounds like the issue lies in passing the data to the view. You might want to try passing the data as an array with a key, like this:


return view('family', ['data' => $data]);
Then in the view, you can access the data array using the 'data' key:

php
<?php

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

print_r($data);
}

?>
This should allow you to access the data array in the view and display the data.
Reply
#6

Yes, that is exactly what I did. :-)

At first I treated $data as variable, but I needed to treat $data as the container of the variables.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB