CodeIgniter视图中未定义的变量 [英] Undefined variable in CodeIgniter View
问题描述
我想在我的CodeIgniter视图中打印我的键/值对数据。但是,我得到以下错误。
遇到PHP错误
<严重性:通知
消息:未定义的变量:data
文件名:views / search_page2.php
行号:8
application / controller / search.php
// ...
$ this-> load-> library 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
<!DOCTYPE html>
< html lang =en>
< head>
< meta charset =utf-8>
< title> Twitter测试< / title>
< / head>
< body>
<?php print_r($ data); >
<?php foreach($ data as $ key => $ value):?>
< h2><?php echo $ key。 ''。 $ value。 '< br />'; ?>< / h2>
<?php endforeach?>
< / body>
< / html>
$ data ['url'] = $ url;
$ data ['getfield'] = $ getfield;
$ data ['requestMethod'] = $ requestMethod;
$ data ['data'] = $ data;
$ this-> load-> view('search_page2',$ data);
否则只有名称为其键的变量将在视图中可用,而不是我们传递的数据变量。
更新:
这是回应您对juan的回答的评论
正在尝试以其他建议的方式工作。
控制器代码将与您发布的代码没有任何变化。
$ data ['url'] = $ url;
$ data ['getfield'] = $ getfield;
$ data ['requestMethod'] = $ requestMethod;
$ this-> load-> view('search_page2',$ data);
但在视图代码中,您只需要执行。
< h2> url<?PHP echo $ url; >< br />< / h2>
< h2> getfield<?PHP echo $ getfield; >< br />< / h2>
< h2> requestMethod<?PHP echo $ requestMethod; >< br />< / h2>
而不是foreach循环作为$ data中的键在视图中已经作为相应的命名变量。
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?
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/search_page2.php
Line Number: 8
application/controller/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
<!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>
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.
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.
这篇关于CodeIgniter视图中未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!