我需要使用从数据库表中提取的数据来填充视图中的一个表。但是,当我尝试此操作时,它返回错误:

Fatal error: Call to a member function result_array() on a non-object in C:\xampp\htdocs\bit\application\views\admin\add_new_room.php on line 32


视图:

<table class="table table-hover">
<?php foreach ($query->result_array() as $row): { ?> // Line 32
<tr>
    <td><?php echo $row['room_number'];?></td>
    <td><?php echo $row['rt_name'];?></td>
    <td><?php echo $row['housekeeping_status'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</table>


控制器:

function index() {
    $this->load->model('admin/edit_other_details');
    $roomType['rt_name'] = $this->edit_other_details->get_room_details();
    $data['query'] = $this->edit_other_details->roomsTable();
    $tmp = array_merge($roomType, $data);
    $this->load->view('/admin/add_new_room', $tmp);
}


模型:

function roomsTable() {
    $query = $this->db->select(array('room_number','rt_name'))->get('rooms');
}

最佳答案

那是因为您忘记了返回模型中的数据。

尝试这个

模型

 function roomsTable() {
   $query = $this->db->select(array('room_number','rt_name'))->get('rooms');
   return $query;  //<---here
}

关于php - CodeIgniter:从数据库(表)填充表(HTML/表单),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17591083/

10-10 08:14
查看更多