Im trying to populate my dropdown with data from my database
Heres my Model :
public function getBuyer($slug = false)
{
if ($slug == false) {
return $this->findAll();
}
return $this->where(['kode_divisi' => $slug])->first();
}
Controllers :
public function produksi()
{
$data = [
'main' => 'buyer/produksi',
'title' => 'List Produksi',
'hasil' => $this->buyerModel->getbuyer()
];
return view('template/template', $data);
}
And View :
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-md-10 col-sm-10 col-xs-10 mx-auto">
<div class="card">
<div class="card-header bg-info" style="text-align: center;">
<h2>Form Tambah Data Pegawai</h2>
</div>
<form action="/pegawai/save" method="post">
<?= csrf_field(); ?>
<div class="form-group">
<label for="kode_divisi" class="col-sm-2 col-form-label">Kode Divisi</label>
<div class="col-sm-12">
<input type="date" name="bday" max="3000-12-31"
min="1000-01-01" class="form-control">
</div>
</div>
<div class="form-group">
<label for="name">Buyer</label>
<div class="controls">
<select required name="kode_gejala">
<option value="" disabled diselected>-- Pilih Buyer --</option>
<?php
foreach ($hasil as $row) {
echo "<option value='".$row->kode_buyer."'>".$row->nama_buyer."</option>";
}
echo"
</select>"
?>
</div>
</div>
<div class="form-group">
<label for="nama_pegawai" class="col-sm-2 col-form-label">Nama Pegawai</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="nama_pegawai" name="nama_pegawai" value="<?= old('nama_pegawai'); ?>">
</div>
</div>
<div class="form-group">
<label for="job_desc" class="col-sm-2 col-form-label">Job Desc</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="job_desc" name="job_desc" value="<?= old('job_desc'); ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-info btn-lg"><i class="fa fa-plus"></i> Tambah Data</button>
<a class="btn btn-danger btn-lg" href="/pegawai" role="button"><i class="fa fa-remove"></i> Batal</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
And it gave my this error:
ErrorException Trying to get property 'kode_buyer' of non-object
it says that this line is the cause of the error:
echo "<option value='".$row->kode_buyer."'>".$row->nama_buyer."</option>";
Is there a way to fix this? i already try some alternate way, but it lead to the same error
Im using Codeigniter 4 BTW.
as by your comment, your data looks like this:
array(3) { ["main"]=> string(14) "buyer/produksi"
["title"]=> string(13) "List Produksi"
["dataku"]=> object(stdClass)#87 (5) {
["kode_buyer"]=> string(5) "IK001"
["nama_buyer"]=> string(13) "Ik Collection" ["pic"]=> string(13) "Wanda Purnomo"
["no_tlp"]=> string(1) "0"
["alamat"]=> string(6) "Cimahi"
}
}
This means kode_buyer
is not at array's top level, but a nested object of dataku
, therefore $row->kode_buyer
gives you an error, as it doesn't exist there. You need to access kode_buyer that way:
$row["dataku"]->kode_buyer