1

我有一个必须从数据库中读取数据的应用程序。我认为这很简单,但为什么我仍然得到这样的错误:

遇到 PHP 错误 严重性:通知消息:未定义变量:数据文件名:views/detail_usulan.php

我已尝试修复此代码,但仍无法正常工作。我会告诉你我的代码。我需要你的帮助:(

我的控制器:

function detail_usulan(){
        $data = array('model_usulan' => $this->model_usulan->get_all());
        //$this->load->view('usulan/detail_usulan', $data);
        $this->render('usulan/detail_usulan', $data);
}

我的模型:

class model_usulan extends MY_Model{
    function get_all(){
        return $this->db->get('usulan_rkau');
    }
}

我的观点 :

<?php foreach ($data as $view_data) { ?>
            <tr>
            <th> Tahun : </th>
            <td><?php echo $view_data->tahun; ?></td>
            </tr>
4

2 回答 2

0

您需要在控制器中进行此更改

function detail_usulan(){
    $data['data'] = array('model_usulan' => $this->model_usulan->get_all());
    $this->load->view('usulan/detail_usulan', $data);
    //$this->render('usulan/detail_usulan', $data);
}

或者

改变你的看法

 foreach ($model_usulan as $view_data) {

希望这会奏效。

于 2016-09-25T05:37:35.247 回答
0

You could just try it like below on controller instead of it begin in an array()

$data['model_usulan'] = $this->model_usulan->get_all();

$this->load->view('usulan/detail_usulan', $data);

On your view change this $data

<?php foreach ($data as $view_data) { ?>
   <tr>
      <th> Tahun : </th>
      <td><?php echo $view_data->tahun; ?></td>
   </tr>
<?php }?>

To $model_usulan in the array

<?php foreach ($model_usulan as $view_data) { ?>
   <tr>
      <th> Tahun : </th>
      <td><?php echo $view_data->tahun; ?></td>
   </tr>
<?php }?>

Note: Your class name and file name should have the FIRST letter upper case ONLY same applies for controllers and libraries etc

As explained here Classnames And Filenames

Filename: Model_usulan.php

<?php

class Model_usulan extends CI_Model {

}

Also On your model function try

return $this->db->get('usulan_rkau')->result();

Or

$query = $this->db->get('usulan_rkau');

return $query->result();

And may be change this

MY_Model {

To

CI_Model {
于 2016-09-25T04:42:18.743 回答