php CodeIgniter 视图中未定义的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16500647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:14:18  来源:igfitidea点击:

Undefined variable in CodeIgniter View

phpcodeigniter

提问by Anthony

I'm trying to print my key/value pairs of data in my CodeIgniter view. However, I'm getting the following error. What I'm I doing wrong?

我正在尝试在我的 CodeIgniter 视图中打印我的键/值对数据。但是,我收到以下错误。我做错了什么?

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/search_page2.php

Line Number: 8

遇到 PHP 错误

严重性:注意

消息:未定义变量:数据

文件名:views/search_page2.php

行号:8

application/controller/search.php

应用程序/控制器/search.php

// ...
        $this->load->library('/twitter/TwitterAPIExchange', $settings);
        $url = 'https://api.twitter.com/1.1/followers/ids.json';
        $getfield = '?username=johndoe';
        $requestMethod = 'GET';     
        $twitter = new TwitterAPIExchange($settings);

        $data['url'] = $url;
        $data['getfield'] = $getfield;
        $data['requestMethod'] = $requestMethod;        

        $this->load->view('search_page2', $data);
// ...

application/views/search_page2.php

应用程序/视图/search_page2.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Twitter Test</title> 
</head>
<body>
<?php print_r($data); ?>

<?php foreach ($data as $key => $value): ?>
    <h2><?php echo $key . ' ' . $value . '<br />'; ?></h2>
<?php endforeach ?>
</body>
</html>

采纳答案by mithunsatheesh

to get the data array accessible in the view do like

要在视图中访问数据数组,请执行以下操作

    $data['url'] = $url;
    $data['getfield'] = $getfield;
    $data['requestMethod'] = $requestMethod;        

    $data['data'] = $data;
    $this->load->view('search_page2', $data);

else only the variables with names as its keys will be available in the view not the data variable we pass.

否则只有名称作为键的变量将在视图中可用,而不是我们传递的数据变量。

update:

更新:

this is in response to your comment to juan's answer Actually if you are trying make it working in the other way proposed.

这是对您对 juan 回答的评论的回应。实际上,如果您尝试使其以建议的其他方式工作。

controller code will be having no change from the code you posted.

控制器代码与您发布的代码没有任何变化。

    $data['url'] = $url;
    $data['getfield'] = $getfield;
    $data['requestMethod'] = $requestMethod;
    $this->load->view('search_page2', $data);

but in the view code you will need to just do.

但在视图代码中,您只需要做。

 <h2>url <?PHP echo $url; ?><br /></h2>
 <h2>getfield <?PHP echo $getfield; ?><br /></h2>
 <h2>requestMethod <?PHP echo $requestMethod; ?><br /></h2>

instead of the foreach loop as your keys in $data are already available as respective named variables inside the view.

而不是 foreach 循环,因为 $data 中的键已经作为视图内的相应命名变量可用。

回答by Juan Mendes

The variables to use in your template are

要在模板中使用的变量是

 $url, $getfield, $requestMethod

$datais the container for the variables that are passed to the view and not accessible directly

$data是传递给视图且不能直接访问的变量的容器

If you do need $dataaccessible to the view, use a different wrapper object

如果您确实需要$data访问视图,请使用不同的包装对象

 $container  = array();
 $container  ['url'] = $url;
 $container  ['getfield'] = $getfield;
 $container  ['requestMethod'] = $requestMethod;
 $container  ['data'] = $data;
 $this->load->view('search_page2', $container);