本文介绍了消息:未定义变量:数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在CodeIgniter中运行以下应用程序时,会出现以下错误:

When I try to run the following application in CodeIgniter, I get the following error:

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?

推荐答案

您必须更改您的控制器和视图

You have to change your controller and view

应该是这样:

$data['post'] = $this->db->get('posts');

并在您的视图中:

<?php foreach($post->result() as $row): ?>
<h1><?php echo $row->title; ?></h1>
<p><?php echo $row->post; ?></p>
<?php endforeach; ?>

codeiginter使用$ data array发送变量到视图。如果你想发送一个视图,把$ data里面$ data ['key'] = $ val;

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;

这篇关于消息:未定义变量:数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:13