[英]Undefined JSON variable in CodeIgniter View
Why am I getting the following errors 为什么我会收到以下错误
A PHP Error was encountered Severity: Warning Message: Undefined variable: json Filename: views/search_page.php Line Number: 8
遇到PHP错误严重性:警告消息:未定义的变量:json文件名:views / search_page.php行号:8
A PHP Error was encountered Severity: Warning Message: Trying to get property of non-object Filename: views/search_page.php Line Number: 8
遇到PHP错误严重性:警告消息:尝试获取非对象的属性文件名:views / search_page.php行号:8
A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: views/search_page.php Line Number: 8
遇到PHP错误严重性:警告消息:为foreach()提供的参数无效文件名:views / search_page.php行号:8
with this code? 用这个代码?
search.php search.php中
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function index()
{
$json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=to%3astackexchange'));
$this->load->view('search_page', $json);
}
}
/* End of file search.php */
/* Location: ./application/controllers/search.php */
search_page.php search_page.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Twitter Test</title>
</head>
<body>
<?php foreach ($json->results as $result): ?>
<h2><?php echo $result->from_user; ?></h2>
<?php endforeach ?>
</body>
</html>
You need to assign the variable you're passing in ( $json
) to a name ("json") 您需要将传入的变量(
$json
)分配给名称(“json”)
$this->load->view('search_page', array('json' => $json));
Perhaps a more clear example: 也许是一个更明确的例子:
$this->load->view('search_page', array('myNeatObject' => $json));
// ...then, in your view, you could
<p>This is the JSON: <?php echo print_r($myNeatObject, true) ?></p>
That's how you name a variable for access in a view. 这就是为视图中的访问命名变量的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.