doubei8168 2015-02-04 11:41
浏览 51
已采纳

CodeIgniter,将数据从模型传递到控制器以在视图中使用

I want to pass data queried in my model to the controller, to do so I am using return $data. Then in the controller I use $this->load->view('my_view', $data);

From my understanding var_dump($data); in the view should show me the results from the query... This is not the case. I am getting "undefined variable data" and NULL from the var_dump($data);.

Here is my model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage_accounts_model extends CI_Model {


    public function index() {

        //

    }

    public function get_users(){

        $data = array();

        $data['query'] = $this->db->get('users');

        return $data['query'];

     }

}

Here is my controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage_accounts extends CI_Controller {

    public function index() {

        $this->load->view('template/header');

        $this->load->model('manage_accounts_model');

        $this->load->view('template/footer');

        $this->load->model('manage_accounts_model');

        $res = $this->manage_accounts_model->get_users();

        if($res){

            $this->load->view('manage_accounts_view', $data);

        } else {

            echo "Fail";

        }

  }

}

And finally my view:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?>
<div class="container">

    <h1><?php if($title){ echo $title; } ?></h1>

    <?php var_dump($data['query']); ?>

</div>
  • 写回答

5条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 苹果app名字侵权商标,我投诉了,但是苹果一直没回怎么办
      • ¥15 Android studio运行不成功总是报错
      • ¥15 pycharm 加载软件包列表时出错connect timed out
      • ¥15 和您碰到了同一个问题
      • ¥15 如何在微信支付V3PHP版请求中添加header?
      • ¥15 在数字信号处理中用MATLAB显示波形。
      • ¥15 keil MDK软件进行仿真调试的局限
      • ¥15 关于gateway产生临时文件夹的问题
      • ¥15 Java Broken Pipe以及OOM
      • ¥20 请问有没有RTL8211FS的参考原理图