CodeIgniter Forums
Passing variable to view results in empty variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Passing variable to view results in empty variable (/showthread.php?tid=87024)



Passing variable to view results in empty variable - Delph1 - 03-06-2023

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?


RE: Passing variable to view results in empty variable - kenjis - 03-06-2023

See https://codeigniter4.github.io/CodeIgniter4/outgoing/views.html#adding-dynamic-data-to-the-view


RE: Passing variable to view results in empty variable - HermyC - 03-06-2023

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);




RE: Passing variable to view results in empty variable - Delph1 - 03-06-2023

(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!


RE: Passing variable to view results in empty variable - KeroniMasingolir - 03-22-2023

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.


RE: Passing variable to view results in empty variable - Delph1 - 03-22-2023

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.