CodeIgniter, pass data from model to controller to

2019-04-13 18:35发布

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条回答
2楼-- · 2019-04-13 19:09

You forgot it

 $data['resultSet']=$res;

Now access the result as $res in your code

查看更多
做自己的国王
3楼-- · 2019-04-13 19:18

cause $data is not defined in your controller try

$data = array();
if($res){
  $data['res'] = $res;
  $this->load->view('manage_accounts_view', $data);
}

Then get on view

<?php var_dump($res); ?>

Also you are sending whole query from model not result for return result you need like :-

public function get_users(){
 $query = $this->db->get('users');
 return $query->result();
}
查看更多
趁早两清
4楼-- · 2019-04-13 19:27

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);
查看更多
够拽才男人
5楼-- · 2019-04-13 19:28

Just Need to do change into controller

$data['query'] = $this->manage_accounts_model->get_users();

if($data){
     $this->load->view('manage_accounts_view', $data);
}else{
     echo "Fail";
}
查看更多
霸刀☆藐视天下
6楼-- · 2019-04-13 19:31

You did not pass the query key variable to view.

change:

$res = $this->manage_accounts_model->get_users();
if($res) {
  $this->load->view('manage_accounts_view', $data);

To:

$data['query'] = $this->manage_accounts_model->get_users();
if($data['query']){
  $this->load->view('manage_accounts_view', $data);
...
查看更多
登录 后发表回答