我试图用数据库中的数据填充下拉列表
这是我的型号:
public function getBuyer($slug = false)
{
if ($slug == false) {
return $this->findAll();
}
return $this->where(['kode_divisi' => $slug])->first();
}
控制器:
public function produksi()
{
$data = [
'main' => 'buyer/produksi',
'title' => 'List Produksi',
'hasil' => $this->buyerModel->getbuyer()
];
return view('template/template', $data);
}
和视图:
<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>
它给了我这个错误:
ErrorException尝试获取非对象的属性"kode_buyer">
它说这一行是错误的原因:
echo "<option value='".$row->kode_buyer."'>".$row->nama_buyer."</option>";
有办法解决这个问题吗?我已经尝试了一些替代方法,但它导致了相同的错误
Im使用Codeigniter 4 BTW.
根据您的评论,您的数据如下所示:
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"
}
}
这意味着kode_buyer
不在数组的顶层,而是dataku
的嵌套对象,因此$row->kode_buyer
会给您一个错误,因为它不存在。您需要以这种方式访问kode_buyer:
$row["dataku"]->kode_buyer