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条回答 默认 最新

  • dryift6733 2015-02-04 11:53
    关注

    I think you made 2 mistakes

    1. forget to fetch the result to an array
    2. forget to pass the data to view

    change in your model class.

    public function get_users(){
    
        $data = array();
        $query = $this->db->get('users');
        $res   = $query->result();        
        return $res;
    
     }
    

    change in your controller class

    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){
            $data['result'] = $res;
            $this->load->view('manage_accounts_view', $data);
    
        } else {
    
            echo "Fail";
    
        }
    

    in your view

      print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行