1

I don't know where the foreach variable came from ... I got it from copypaste from my previous program

Message: Undefined variable: barang

Message: Invalid argument supplied for foreach()


this is my view eror

                                 <?php
                                        foreach($barang as $i): ?>
                                        <tr>
                                            <td><?=$i->id_barang;?></td>
                                            <td><?=$i->id_jenis;?></td>
                                            <td><?=$i->nm_barang;?></td>
                                            <td><?=$i->stok;?></td>
                                            <td><?=$i->hrg_beli;?></td>
                                            <td><?=$i->jual;?></td>

                                    </tbody>
                           <?php endforeach ?>


this is my controller to show the table

public function __construct(){
parent::__construct();
$this->load->model('model_barang', 'barang');}

public function index(){
  $data['barang'] = $this->barang->tampil_data();
  $this->load->view('view');}


this is my model to show the table

function tampil_data(){
    $data = $this->db->get('barang');
    return $data;
}
tereško
  • 58,060
  • 25
  • 98
  • 150
xaddam lee
  • 29
  • 3

2 Answers2

0

You should pass data variable in view file

public function index(){
  $data['barang'] = $this->barang->tampil_data();
  $this->load->view('view',$data);
}
function tampil_data(){
 $data = $this->db->get('barang');
 return $data->result(); 
 } 

In view file (as per database field)

<?php foreach($barang as $i){ ?>
 <td><?php echo $i->id;?></td>
<?php } ?>
0

add ->result_array() in your query

function tampil_data(){
 $data = $this->db->get('barang')->result_array();
 return $data; 
}  

and pass $data variable

 $this->load->view('view',$data);
  • this makes a little change in my table. I replace result_array (); with result (); and the results are pretty good even though there are still errors. Thank you very much – xaddam lee Aug 16 '18 at 06:17