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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28320504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:47:52  来源:igfitidea点击:

CodeIgniter, pass data from model to controller to use in view

phpcodeigniterscopevariable-assignment

提问by Richard

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);

我想将在我的模型中查询的数据传递给控制器​​,为此我使用了 return $data。然后在我使用的控制器中$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);.

根据我var_dump($data);在视图中的理解应该向我显示查询的结果......事实并非如此。我从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>

回答by Tintu C Raju

I think you made 2 mistakes

我认为你犯了两个错误

  1. forget to fetch the result to an array
  2. forget to pass the data to view
  1. 忘记将结果提取到数组中
  2. 忘记传数据查看

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);

回答by Rakesh Sharma

cause $datais not defined in your controller try

原因$data未在您的控制器中定义尝试

$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();
}

回答by Danyal Sandeelo

You forgot it

你忘记了

 $data['resultSet']=$res;

Now access the result as $res in your code

现在在您的代码中以 $res 的形式访问结果

回答by Pupil

You did not pass the querykey variable to view.

您没有传递query要查看的关键变量。

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);
...

回答by Ghanshyam Dekavadiya

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";
}