Codeigniter - 在表单编辑中未显示数组时制作下拉菜单
Posted
技术标签:
【中文标题】Codeigniter - 在表单编辑中未显示数组时制作下拉菜单【英文标题】:Codeigniter - Making dropdown menu while array didn't showing in form edit 【发布时间】:2018-07-13 03:23:22 【问题描述】:我是 codeigniter 的新手,我对此一无所知。它在另一个视图(表单添加)中工作,而这个(表单编辑)没有工作。 并且没有任何错误通知。
我的情况是我想在表单编辑中显示数据库中的下拉菜单,就像在表单添加中一样。
这是我的代码(用于添加表单的代码):
控制器 Admin.php : 用于添加数据
class Admin extends CI_Controller
public function tambah_data()
$this->load->helper('form');
//kategori
$this->load->model('models_kategori_barang', 'mkb');
$datakb = $this->mkb->GetKategoriBarang();
//model
$this->load->model('models_model_barang', 'modb');
$datamdb = $this->modb->GetModelBarang();
//material
$this->load->model('models_material_barang', 'matb');
$datamatb = $this->matb->GetMaterialBarang();
//merk
$this->load->model('models_merk_barang', 'merb');
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/tambah_data',array(
'datakb' => $datakb,
'datamdb' => $datamdb,
'datamatb' => $datamatb,
'datamerb' => $datamerb));
查看tambah_data.php
<?php echo form_open_multipart('crud_barang/do_insert'); ?>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" autofocus required></label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" required></label>
<label>Kategori<br>
<select name="kategori_barang" required>
<option disabled selected>Pilih Katergori...</option>
<?php foreach ($datakb as $dkb) ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php ?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option disabled selected>Pilih Model...</option>
<?php foreach ($datamdb as $dmdb) ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php ?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option disabled selected>Pilih Material...</option>
<?php foreach ($datamatb as $dmtb) ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php ?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option disabled selected>Pilih Merk...</option>
<?php foreach ($datamerb as $dmb) ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php ?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" required></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" required></label>
<label>Keterangan<br>
<textarea placeholder="Keterangan" name="ket_barang"></textarea>
</label>
<p><input type="submit" value="Masukan" class="btn"></p>
<?php echo form_close(); ?>
模型models_barang.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Models_barang extends CI_Model
public function GetBarang($where="")
$datab = $this->db->get('barang'.$where);
return $datab->result_array();
public function InsertData($tabelName, $datab)
$res = $this->db->insert($tabelName,$datab);
return $res;
public function UpdateData($tabelName,$datab,$where)
$res = $this->db->update($tabelName,$datab,$where);
return $res;
public function DeleteData($tabelName,$where)
$res = $this->db->delete($tabelName,$where);
return $res;
当我将它放入表单编辑时,这个不起作用:
控制器 crud_barang.php
class crud_barang extends CI_Controller
//funciton insert was here
public function edit_data($id_barang)
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang'=> $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
$this->load->view('templates/admin/form_edit_barang',$datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/form_edit_barang',array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb), TRUE);
public function do_update()
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$id_barang = $_POST['id_barang'];
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$material_barang = $_POST['material_barang'];
$model_barang = $_POST['model_barang'];
$kategori_barang = $_POST['kategori_barang'];
$harga_barang = $_POST['harga_barang'];
$size_barang = $_POST['size_barang'];
$merk_barang = $_POST['merk_barang'];
$ket_barang = $_POST['ket_barang'];
$data_update = array(
'id_barang' => $id_barang,
'kode_barang' => $kode_barang,
'nama_barang' => $nama_barang,
'material_barang'=> $material_barang,
'model_barang' => $model_barang,
'kategori_barang' => $kategori_barang,
'harga_barang' => $harga_barang,
'size_barang' => $size_barang,
'merk_barang' => $merk_barang,
'ket_barang' => $ket_barang
);
$where = array('id_barang' => $id_barang);
$res = $this->mb->UpdateData('barang', $data_update, $where);
if($res>=1)
$this->session->set_flashdata('pesan_barang','- Update Data Barang Sukses');
redirect('admin/lihat_data');
else
echo "<h2>Update Data Barang Gagal</h2>";
查看 form_edit_barang.php
<?php echo form_open_multipart('crud_barang/do_update'); ?>
<label>Id Barang<br>
<input type="text" autofocus placeholder="Id Barang" name="id_barang" value="<?php echo $id_barang; ?>" readonly>
</label>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" value="<?php echo $kode_barang; ?>">
</label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" value="<?php echo $nama_barang; ?>">
</label>
<label>Kategori<br>
<select name="kategori_barang">
<option><?php echo $kategori_barang;?></option>
<?php foreach ($datakb as $dkb) ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php ?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option><?php echo $model_barang;?></option>
<?php foreach ($datamdb as $dmdb) ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php ?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option><?php echo $material_barang;?></option>
<?php foreach ($datamatb as $dmtb) ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php ?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option><?php echo $merk_barang;?></option>
<?php foreach ($datamerb as $dmb) ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php ?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" value="<?php echo $harga_barang; ?>"></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" value="<?php echo $size_barang; ?>"></label>
<label>Keterangan<br><textarea placeholder="Keterangan" name="ket_barang""><?php echo $ket_barang; ?></textarea></label>
<p><input type="submit" value="Simpan" class="btn"></p>
<?php echo form_close(); ?>
数据库中的下拉菜单数据不会显示在表单编辑中。我不明白为什么,我错过了什么还是什么?因为没有错误通知。请帮帮我,谢谢:)
表格添加的结果(工作): 1
在表单编辑时,只显示原始数据: 2
【问题讨论】:
【参考方案1】:我猜是因为在您的edit_data
函数中,您加载了两次视图。为什么这没有给你未定义的$id_barang
.etc 的通知。超出了我的范围;也不知道为什么你没有看到两次视图(可能是因为你有错误被抑制)。
请注意cmets。
public function edit_data($id_barang)
/* load model dahulu, karena bukan global model */
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang' => $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
// DUPLICATE, REMOVED
//$this->load->view('templates/admin/form_edit_barang', $datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
// MERGED ARRAY HERE
$this->load->view('templates/admin/form_edit_barang', array_merge($datab, array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb)));
【讨论】:
嗨,亚历克斯!谢谢您的回答。我加载了两次视图,因为我已经尝试了很多方法,将它放在一个视图中。我今天才知道array_merge。所以谢谢你..它的工作:)以上是关于Codeigniter - 在表单编辑中未显示数组时制作下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ajax (codeigniter) 在我的编辑表单中获取和显示选定值到 <select2> 标记中
CodeIgniter 中未显示验证 flashdata 错误
Codeigniter:如何构建使用表单验证和重新填充的编辑表单?