wzhw666 发表于 2009-3-19 16:59:04

大侠救急呀~~~为什么循环不出来我要的数据呢

这个是我的controller下的test.php的代码:
<?php

class Test extends Controller {

    function Test()
    {
      parent::Controller();   

      $this->load->scaffolding("table");
    }
   
    function index()
    {
      $data = $this->load->model('Test_mod');
      $this->load->view('test_view',$data);
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
?>

下面是我models下的test_mod.php文件:
<?php

class Test_mod extends Model {

    function Test_mod()
    {
      parent::Model();   
    }
   
    function index()
    {
      $query = $this->db->get('table');
      return $query;
    }
}
?>

下面是views下的test_view.php文件:
<html>
<head>
<title>CodeIgniter for test</title>

<?php foreach($data->result() as $row):?>

<h5><?=$row->id?></h5>
<h6><?=$row->name?></h6>
<?php endforeach; ?>

</body>
</html>


为什么运行之后出现如下的报错:

A PHP Error was encounteredSeverity: Notice
Message:Undefined variable: data
Filename: views/test_view.php
Line Number: 5


Fatal error:Call to a member function result() on a non-object in D:\xampp\htdocs\CodeIgniter\system\application\views\test_view.php on line 5

wzhw666 发表于 2009-3-19 17:01:53

这个我是模仿那个二十分钟建blog的视频写的啊,为什么在我这里就不行呢?
帮忙看看吧,谢谢了。

Hex 发表于 2009-3-19 17:06:26

让你看 看 http://codeigniter.org.cn/user_guide/database/results.html
怎么就是不看呢?

kissmumu 发表于 2009-3-19 18:39:36

一,你的db是auto loading么?
二,你的controller里调用model时,并没有调用到你的model里的index(),这样也没有数据返回啊

如果db已经加载了,那么把controller的index改成:
$data = $this->load->model('Test_mod');
$data = $this->Test_mod->index();//你没有这一句
$this->load->view('test_view',$data);

希望可以帮到你:)

wzhw666 发表于 2009-3-20 08:52:31

:victory:谢谢各位
页: [1]
查看完整版本: 大侠救急呀~~~为什么循环不出来我要的数据呢