php 消息:未定义变量:数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7905403/
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
Message: Undefined variable: data
提问by johntheripper
When I try to run the following application in CodeIgniter, I get the following error:
当我尝试在 CodeIgniter 中运行以下应用程序时,出现以下错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/blog.php
Line Number: 1
I've been trying to figure it out for almost an hour and I can't get it to work. My view looks like this:
我一直试图弄清楚它将近一个小时,但我无法让它工作。我的观点是这样的:
<?php foreach($data->result() as $row): ?>
<h1><?php echo $row->title; ?></h1>
<p><?php echo $row->post; ?></p>
<?php endforeach; ?>
My controller looks like this:
我的控制器看起来像这样:
<?php
class Blog extends CI_Controller {
public function index()
{
$this->load->database();
$data = $this->db->get('posts');
$this->load->helper('url');
$this->load->view('header');
$this->load->view('blog', $data);
$this->load->view('footer');
}
}
Anyone know how to fix this?
有人知道怎么修这个东西吗?
回答by onatm
You have to change your controller and view
你必须改变你的控制器和视图
the array you send throught data should be like this:
您通过数据发送的数组应该是这样的:
$data['post'] = $this->db->get('posts');
and in your view:
在您看来:
<?php foreach($post->result() as $row): ?>
<h1><?php echo $row->title; ?></h1>
<p><?php echo $row->post; ?></p>
<?php endforeach; ?>
codeiginter sends variables to view using $data array. If you want to send something to a view, put inside to $data as $data['key'] = $val;
codeiginter 使用 $data 数组发送变量以查看。如果您想向视图发送某些内容,请将其放入 $data 作为 $data['key'] = $val;
回答by powtac
Try to use $blog
instead of $data
in the first line of your view.
I'm not sure but you assign $data
to a key called blog
in your controller...
尝试在视图的第一行使用$blog
而不是$data
。
我不确定,但您分配$data
给blog
控制器中调用的键...
回答by vicentazo
回答by Jhourlad Estrella
I think the error notice is not originating on your controller but on your view (blog.php). You forgot to pass $data to the view. You should restructure the variable being passed to your view to something like this:
我认为错误通知不是源自您的控制器,而是源自您的视图 (blog.php)。您忘记将 $data 传递给视图。您应该将传递给您的视图的变量重构为如下所示:
$data['data'] = $this->db->get('posts');
$this->load->view('blog', $data);
回答by IAMSTR
please structure your post model like this
请像这样构建您的帖子模型
public function __construct()
{
$this->load->database();
}
public function get_posts(){
$query=$this->db->get('posts');
return $query->result_array();
}
}
and your post controller like this
和你这样的帖子控制器
public function index()
{
$data['posts']=$this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index.php', $data);
$this->load->view('templates/footer');
}
and in your view file echo content this way :)
并在您的视图文件中以这种方式回显内容:)
<?php foreach($posts as $post): ?>
<h3><?php echo $post['post_title'];?></h3>
<small><?php echo $post['post_date'];?></small>
<p><a href="<?php echo site_url('/posts/'.$post['post_title']);?>">Read more</a></p>
<?php endforeach;?>