0

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?

1
  • the PHP tags are incorrect.This is correct <?=var_dump($n);?> Commented Jan 28, 2016 at 20:10

5 Answers 5

2

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:
?>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

Use this in your view once.

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

Comments

0

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


        ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.