codeigniter 我写代码有什么问题吗?

ltskdhd1  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(109)

我是程序初学者
我尝试从CI 3中其他模型获取数据
可以帮助我修复此错误吗?
错误
消息:未定义的变量:数据文件名:邮箱/sale_form. php274
我的观点:

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

请帮助我修复此错误
我被困在这里,我决定问在这里

f0brbegy

f0brbegy1#

在控制器中,您应该更换

$data = ['item' => $item];

$data = ['data' => $item];

或者

$item = $this->item_m->get()->result();
$this->template->load('template','transaction/sale/sale_form', array('data' => $item));
uqzxnwby

uqzxnwby2#

## 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);
$data['itemData'] = $item;
从控制器,打印_r($data ['itemData']);
从您的视图/模板,print_r($itemData);
http://www.codeigniter.com/userguide3/general/views.html

相关问题