【问题标题】:To display data stored on the dropdown menu显示存储在下拉菜单中的数据
【发布时间】:2019-01-17 07:39:20
【问题描述】:

我的下拉菜单中显示的结果有问题。对于下拉菜单,我不直接选择与出现的 id 匹配的类型。

如何让下拉菜单根据item ID出现?

此视图

label for="type">Type</label>
                                <select class="form-control" id="type_id" name="type_id">
                                    <?php foreach($type as $types) : ?>
                                        <?php if( $types == $barang['type_id']) : ?>
                                            echo '<option value="<?php echo "$types->type_id"?>" selected><?php echo "$types->type_nama"?></option>';
                                        <?php else : ?>
                                            echo '<option value="<?php echo "$types->type_id"?>"><?php echo "$types->type_nama"?></option>';
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>

这个模型

public function getTypeQuery()
{
    $this->db->order_by('type_id', 'asc');
    $query = $this->db->get('type');
    return $query->result();
}

public function getTypeById($id)
{
    return $this->db->get_where('barang', ['barang_id' => $id])->row_array();
}

public function ubahTypeBarang ()
{
    $data = [
        "barang_kode"=> $this->input->post('barang_kode', true),
        "type_id"=> $this->input->post('type_id', true),
        "barang_nama"=> $this->input->post('barang_nama', true),
    ];

    $this->db->where('barang_id', $this->input->post('barang_id'));
    $this->db->update('barang', $data);

}

此控制器

public function ubah_type($id)
{
    $data['judul'] = 'Form Ubah Data Mahasiswa';
    $data['barang'] = $this->Gudang_model->getTypeById($id);
    $data['type'] = $this->Gudang_model->getTypeQuery();

    $this->form_validation->set_rules('barang_kode', 'Kode', 'required');
    $this->form_validation->set_rules('type_id', 'Type', 'required');
    $this->form_validation->set_rules('barang_nama', 'Nama Barang', 'required');

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('templates/header', $data);
        $this->load->view('gudang/ubah_type', $data);
        $this->load->view('templates/footer');  
    } else {
        $this->Gudang_model->ubahTypeBarang();
        $this->session->set_flashdata('flash', 'Diubah');
        redirect('gudang/lihat_type');
    }

}

`

【问题讨论】:

    标签: javascript php codeigniter codeigniter-3


    【解决方案1】:

    尝试像这样更改视图:

    <label for="type">Type</label>
    <select class="form-control" id="type_id" name="type_id">
        <?php foreach($type as $types) : ?>
            <?php if( $types->type_id == $barang['type_id']) : ?>
                <option value="<?php echo $types->type_id ?>" selected><?php echo $types->type_nama ?></option>
            <?php else : ?>
                <option value="<?php echo $types->type_id ?>"><?php echo $types->type_nama ?></option>
            <?php endif; ?>
        <?php endforeach; ?>
    </select>
    

    【讨论】:

    • 不行,下拉菜单变空
    • 嗨@YusufReza,你有没有注意到你在php标签外呼应下拉项?
    • 从不,还有其他解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多