消息:未定义变量:检查 [英] Message: Undefine variable: exam

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

问题描述

控制器:Test.php

controller: Test.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller 
{
    function __construct() 
    {
        parent :: __construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('Fetch_data');
    }
    public function agriculture_exam()
    {
        $stream = 'agriculture';
        $data['exam'] = $this->Fetch_data->top_agriculture_exams($stream);
        $this->load->view('header',$data);
    }
}

view:header.php

view: header.php

<ul class="list">
<?php 
    foreach($exam as $row) 
    {
       echo "<li><a href='#'>".$row['exam_name']."</a></li>"; 
    }
?>
</ul>

型号:Fetch_data.php

model: Fetch_data.php

<?php  
    class Fetch_data extends CI_Model  
    {  
        function __construct()  
        {   
            parent::__construct();  
        }  
        public function top_agriculture_exams($stream)
        {
            $this->db->select('exam_name');
            $this->db->from('all_exams_details');
            $this->db->where('field',$stream);
            $this->db->order_by('exam_name');
            $this->db->limit('10');
            $query = $this->db->get(); 
            $result = $query->result_array();
            return $result;
        }
    }

我是ci的新手。在控制器(即Test.php)中,我定义了$ stream ='agriculture',并将$ stream变量传递给top_agriculture_exams模型。现在,当我在header.php文件中获取数据时,它显示一条消息:未定义变量:$ exam我不知道为什么。因此,我该如何解决?请帮助我。

I am new in ci. In controller i.e. Test.php I am defining $stream='agriculture' and pass $stream variable to top_agriculture_exams model. Now, when I am fetching data on header.php file it show me a Message: Undefine variable: $exam I don't know why. So, how can I fix it ?please help me.

谢谢

推荐答案

而不是使用元素数组访问它,而是尝试使用对象访问它。

instead of accessing it using the element array try accessing it using object.

在Fetch_data.php文件中的代码下面进行更改。

change below code in your Fetch_data.php file.

//$result = $query->result_array();
$result = $query->result();

并通过header.php中的对象进行访问。下面是一个示例。

and access it by object in your header.php. here's an example below.

<ul class="list">
 <?php foreach($exam as $row): ?> 
      <?=  "<li><a href='#'>" . $row->exam_name . "</a></li>" ?>
 <?php endforeach;
</ul>

其中exam_name等于数据库中的列。

where exam_name is equal to the column in your database.

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

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