Reputation: 1
I'm trying to send a array data from a controller to a view. but, a log of the framework accuse: Message: Undefined variable: var.
this is the "fake data" of my controller, the "n":
class Produtos extends CI_Controller{
public function index()
{
$n= [];
$form = [
"greetings" => "welcome",
"user" => "Name:",
"password" => "Pass:",
"copyright" => "2016"
];
array_push($n, $form);
$dados = ["n" => $n];
$this->load->view("n/index.php");
}
}
and, my view named "n" have only a var_dump:
<?= var_dump($n);>
someone can help?
Upvotes: 0
Views: 109
Reputation: 464
You didnot have passed the array into the view page.
<?php
$n= [];
$form = [
"greetings" => "welcome",
"user" => "Name:",
"password" => "Pass:",
"copyright" => "2016"
];
array_push($n, $form);
$dados = ["n" => $n];
$this->load->view("index.php",$dados);
?>
Upvotes: 0
Reputation:
You can check out CodeIgniter documentation . You should pass your data to the view, as second parameter to the $this->load->view
function
$form = [
"greetings" => "welcome",
"user" => "Name:",
"password" => "Pass:",
"copyright" => "2016"
];
$this->load->view("n/index.php", ["n" => $form]);
Then, your var_dump
code is not correct, you should use php tags in a proper way
<?=var_dump($n)?>
Upvotes: 2
Reputation:
You can just user normal array like below
class Produtos extends CI_Controller{
public function index()
{
$data = array(
"greetings" => "welcome",
"user" => "Name:",
"password" => "Pass:",
"copyright" => "2016"
);
$this->load->view("view_file_name", $data);
}
}
I would not use index.php as a name for a view file because you have index.php file in main directory
And then
<?php echo $copyright;?>
Upvotes: 0
Reputation: 5796
You have to use the elements from your passed array:
In your controller:
$this->load->view("n/index.php", $form);
In your view:
<?php
echo $greetings; // welcome
echo $user; // Name:
?>
Upvotes: 2