Codeigniter - 在数组未在表单编辑中显示时进行下拉菜单

时间:2018-02-02 15:25:37

标签: php html html5 forms codeigniter

我是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(); ?>

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

当我把它放在表单编辑中时,这个不起作用:

controller 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(); ?>

来自数据库的下拉菜单数据不会在表单编辑中显示。我不明白为什么,我会错过什么或什么?因为没有错误通知。帮帮我吧,谢谢你:)。

表单add(working)中的结果: 1

在表单编辑时,只显示原始数据: 2

1 个答案:

答案 0 :(得分:0)

我猜它是因为在你的@RealmModule(classes={Dog.class, Cat.class}) public class BundledRealmModule { } 函数中,你正在加载你的视图两次。为什么没有给出未定义edit_data .etc的通知。超越我;也不确定为什么你没有看到两次视图(可能是因为你有错误被抑制)。

请注意评论。

$id_barang