消息:未定义的变量:在“查看”页面中 [英] Message: Undefined variable: in View page

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

问题描述

我还是PHP和MVC概念的新手。我一直在尝试复制CI新闻教程(http://codeigniter.com/user_guide/tutorial/news_section.html),同时使用自己的数据库并重写代码。

I am still new at PHP and the MVC concept. I have been trying to duplicate the CI News Tutorial(http://codeigniter.com/user_guide/tutorial/news_section.html), while using my own database and rewriting the code.

我一直没有成功。

这是我的控制器:

    class Main extends CI_Controller
{

public function __construct()
{
    parent::__construct();

    $this->load->helper('url');
    $this->load->library('tank_auth');
    $this->load->model('structures_model');
}

public function structures()
{
    $this->load->model('structures_model');
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else {
        $data['structures_all'] = $this->structures_model->get_structures();
        $this->load->view('templates/header', $data);
        $this->load->view('templates/navmain', $data);
        $this->load->view('structures', $data);
        $this->load->view('templates/footer', $data);
    }
}

这是我的模型(structures_model)

Here is my model (structures_model)

<?php
class Structures_model extends CI_Model {

public function __construct()
{
    $this->load->database();
}

public function get_structures()
{
    $query = $this->db->get('structures');
    return $query->result_array();
}

}

我的观点:

<?php foreach ($structures_all as $structures_info): ?>
<h2>Structures</h2>
<div id="main">
    <?php echo $structures_info['str_name'] ?>
</div>
<?php endforeach ?>

输入错误是很常见的:

A PHP Error was encountered<
Severity: Notice
Message: Undefined variable: structures_all
Filename: main/structures.php
Line Number: 2

我很茫然。我查看了人们遇到的所有类似错误,但无法弄清楚为什么未定义structure_all数组。不应该在我设置的控制器函数中创建它:

I am at a loss. I have looked at all the similar errors people have gotten, but can't figure out why exactly the structure_all array is not being defined. Shouldn't it get created in the controller function where I set :

$data['structures_all'] = $this->structures_model->get_structures();

我想念什么?

推荐答案

调试此问题的最佳方法是为 $ data ['structures_all'] 分配一个确定的数组值,例如: $ data ['structures_all'] = array('foo'=>'bar');

The best way to debug this would be to assign $data['structures_all'] a definite array value, say: $data['structures_all'] = array('foo' => 'bar');

$ structures_all 变量现在在视图中可用吗?如果可用,您就会知道 $ this-> structures_model-> get_structures(); 返回的是 null

Is the $structures_all variable available in the view now? If it is available, you know that $this->structures_model->get_structures(); is returning null.

数据库中是否有一个名为结构的表?

Do you have a table in your database called structures?

您确定您的数据库连接详细信息已填写在 config / database.php 中吗?

Are you sure your database connection details are filled out in config/database.php?

您是否将php错误报告设置为全部?可能存在隐藏的消息...在控制器的构造函数中调用 error_reporting(E_ALL);

Do you have php error reporting set to all? There might be hidden messages... call error_reporting(E_ALL); in the constructor of your controller.

也尝试回显: $ this-> db-> last_query(); 来验证您的查询正在以与您在phpmyadmin中尝试的方式相同的方式构造。

Also try echoing: $this->db->last_query(); to verify your query is being constructed the same way you tried in phpmyadmin...

希望这会让您走上正确的轨道。

Hopefully this puts you on the right track.

这篇关于消息:未定义的变量:在“查看”页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆