how to get data into view from controller in cakephp

1.4k Views Asked by At
$this->loadModel('Siteconfig');

$data=$this->Siteconfig->find('all');

foreach($data as $data)
{           
    $email=$data['Siteconfig']['email'];

    $companyname=$data['Siteconfig']['companyname'];

    $cfa=$data['Siteconfig']['cfa'];
}

Above is Controller

How to get cfa data in view using CAKEPHP?

2

There are 2 best solutions below

2
On BEST ANSWER

In your controller method, you can use this

$this->set('var_name_in_view', $cfa);  //You will find the variable $cfa as $var_name_in_view in your view(You have to use $var_name_in_view in view)

You can also send multiple variables to your view at once

$this->set(compact('cfa', 'another_variable'));  //You can acceess using $cfa and $another_variable

Compact makes an array keeping the variable name as key and variable value as the value for the array key.

2
On

Modify your script to

$this->loadModel('Siteconfig');

$data=$this->Siteconfig->find('all');
$cfa = '';

foreach($data as $datum)
{           
    $email=$datum['Siteconfig']['email'];

    $companyname=$datum['Siteconfig']['companyname'];

    $cfa=$datum['Siteconfig']['cfa'];
}
$this->set('view_cfa',$cfa);

This will fix it. Your foreach was reassigning $data.