简体   繁体   English

Codeigniter返回未定义的变量codeigniter

[英]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. 但是,该框架的日志指控:消息:未定义的变量:var。

this is the "fake data" of my controller, the "n": 这是我的控制器的“伪数据”,即“ 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: 并且,我的名为“ n”的视图只有一个var_dump:

<?= var_dump($n);>

someone can help? 有人可以帮忙吗?

You can check out CodeIgniter documentation . 您可以查看CodeIgniter文档 You should pass your data to the view, as second parameter to the $this->load->view function 您应该将数据作为$this->load->view函数的第二个参数传递给$this->load->view

$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代码不正确,您应该以适当的方式使用php标签

<?=var_dump($n)?>

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

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 我不会将index.php用作视图文件的名称,因为您在主目录中有index.php文件

And then 接着

<?php echo $copyright;?>

在您的视图中使用一次。

<?= var_dump($n[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);


        ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM