我是程序的初学者
我尝试从 CI3 中的其他模型获取数据
可以帮我解决这个错误吗?
错误
消息:未定义的变量:数据 文件名:sale/sale_form.php 行号:274
我的看法:
<?php foreach ($data as $i => $data) { ?>
<tr>
<td><?=$data->barcode ?></td>
<td><?=$data->name ?></td>
</tr>
<?php } ?>
我的控制器:
$this->load->model('item_m');
$item = $this->item_m->get()->result();
$data = ['item' => $item];
$this->template->load('template','transaction/sale/sale_form', $data);
我的模型项目:
public function get($id = null)
{
$this->db->select('p_item.*,p_category.name as category_name, p_unit.name as unit_name');
$this->db->from('p_item');
$this->db->join('p_category','p_category.category_id = p_item.category_id');
$this->db->join('p_unit','p_unit.unit_id = p_item.unit_id');
if($id != null) {
$this->db->where('item_id', $id);
}
$this->db->order_by('barcode','asc');
$query = $this->db->get();
return $query;
}
请帮我解决这个错误
我被困在这里,我决定在这里问
在控制器中,你应该更换
$data = ['item' => $item];
到
$data = ['data' => $item];
或
$item = $this->item_m->get()->result();
$this->template->load('template','transaction/sale/sale_form', array('data' => $item));
## from your controller ##
$data = ['item' => $item];
$this->template->load('template','transaction/sale/sale_form', $data);
## your view should be ##
<?php foreach ($item as $i => $data) : ?>
<?php endforeach: ?>
$data = ['item' => $item];
从您的控制器,您可以通过 print_r($data['item']);
访问您的数据 但是从您的视图/模板中,您可以通过数组名称访问它,因此, print_r($item);