skip to Main Content

im beginer in program

i try get data from other model in CI3

can help me for fix this error?

the error

Message: Undefined variable: data
Filename: sale/sale_form.php
Line Number: 274

My view:

 <?php foreach ($data as $i => $data) { ?>
    <tr>
     <td><?=$data->barcode ?></td>
      <td><?=$data->name ?></td>
    </tr>
    <?php } ?>

my controller:

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

my model item:

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

pls help me for fix this error

im stuck here, and i decide ask in here

2

Answers


  1. In controller, you should replace

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

    to

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

    Or

    $item = $this->item_m->get()->result();
    $this->template->load('template','transaction/sale/sale_form', array('data' => $item));
    
    Login or Signup to reply.
  2. ## 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];
    from your controller you can access your data by print_r($data[‘item’]);
    but from your view/template you access it by the array name hence, print_r($item);

    $data['itemData'] = $item;
    from controller, print_r($data[‘itemData’]);
    from your view/template, print_r($itemData);

    http://www.codeigniter.com/userguide3/general/views.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search