duanjia7607 2016-01-26 17:33
浏览 42
已采纳

CodeIgniter将模型数据传递给视图[重复]

This question already has an answer here:

I have a model method which returns time and view to pass the data. But when I pass the data I get Undefined variable error message.

$this->load->model('Driver_model');
$result = $this->Driver_model->get_shift_data($id);

This is the method calling and if I use var_dump($result). I get following result.

array (size=1)
  0 => 
    object(stdClass)[26]
      public 'start_time' => string '2016-01-22 05:32:42' (length=19)

But if I send the $result to the view, I get error.

$this->load->view('driver/report_view.php', $result);

My question is, how it possible to work in controller if I don't load the view. But if I send the data, it doesn't identify the variable.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: driver/report_view.php

Line Number: 52

Backtrace:

File: C:\wamp\www\cabs\application\views\drivereport_view.php
Line: 52
Function: _error_handler

File: C:\wamp\www\cabs\application\controllers\Driver.php
Line: 44
Function: view

File: C:\wamp\www\cabs\index.php
Line: 292
Function: require_once

I have tested it using var_dump($result) in view as well.

      <div class="panel-body">
Line 52    <?php var_dump($result); ?>                              
        <form>
</div>
  • 写回答

1条回答 默认 最新

  • dongtan6695 2016-01-26 17:47
    关注

    You can not pass a simple variable $result in load view function for view.

    You need to create an associative array for sending data to view from controller. You can use as:

    $this->load->model('Driver_model'); $data['result'] = $this->Driver_model->get_shift_data($id);
    

    Now send $data array to your view as:

    $this->load->view('driver/report_view.php', $data);
    

    Now you can use var_dump() in view file as:

    <?php var_dump($result); ?>
    

    Note that if you want to send any other data from controller to view you can follow the same example.

    For more explanation see this example:

    $data['result'] = 1;
    $data['result2'] = 2;
    $data['result3'] = 3;
    
    $this->load->view('yourview', $data);
    

    In view you can get data as:

    echo $result; // 1
    echo $result2; // 2
    echo $result3; // 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算