Jonathan Silva
Jonathan Silva

Reputation: 1

Codeigniter return Undefined variable codeigniter

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

Answers (5)

Jibin
Jibin

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

Abhinav Chintakindi
Abhinav Chintakindi

Reputation: 41

Use this in your view once.

<?= var_dump($n[0]) ?>

Upvotes: 0

user4962466
user4962466

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

user4419336
user4419336

Reputation:

You can just user normal array like below

The Loading Of Views

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

schellingerht
schellingerht

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

Related Questions