无法从数据库获取数据下拉列表

时间:2021-03-02 02:37:26

i want to make an edit form. In this edit form, i want make a dropdown that data input pick from database. but when click the dropdown, data doesn't not appear at all. error like this whats wrong? controller :

我想制作一个编辑表格。在这个编辑表单中,我想要一个数据输入从数据库中选择的下拉列表。但是当点击下拉列表时,数据根本不会出现。像这样的错误是什么错?控制器:

public function edit_data($id_artikel='',$foto=''){
    $artikel = $this->mymodel->getedit($id_artikel);
    $data = array(
        "id_artikel"        => $artikel[0]['id_artikel'],
        "judul"             => $artikel[0]['judul'],
        "kategori_terpilih" => $artikel[0]['nama_kategori'],
        "isi"               => $artikel[0]['isi'],
        "foto"              => $artikel[0]['foto']
    );
    $kategori = $this->mymodel->get_kategori();
    $a['kategori'] = $kategori->result();
    $this->load->view('form_edit',$data);
    }

public function do_update(){
        $config['upload_path'] = 'images/uploaded/';
        $config['allowed_types'] ='gif|jpg|png';

        $this->load->library('upload',$config);

        if(!$this->upload->do_upload('foto')){
            $sub_data['error']=$this->upload->display_errors();
            echo '<pre>';
            print_r($sub_data);
            echo '</pre>';

            die;    
        }else{
            $result_upload=$this->upload->data();
        }

    $id_artikel     = $_POST['id_artikel'];
    $judul          = $_POST['judul'];
    $id_kategori    = $_POST['id_kategori'];
    $isi            = $_POST['isi'];
    $foto           = $result_upload['file_name']; 
    $data_update    = array(
    'id_artikel'    => $id_artikel,
    'judul'         => $judul,
    'id_kategori'   => $id_kategori,
    'isi'           => $isi,
    'foto'          => $foto);
    $where = array('id_artikel' => $id_artikel);
    $res = $this->mymodel->UpdateData('artikel',$data_update,$where);
    if($res>=1){
        $this->session->set_flashdata('pesan','Update Data Sukses');
        redirect('crud/index');
    }
    }

model :

型号:

public function getedit($id_artikel=''){
    $data = $this->db->query('SELECT a.id_artikel, a.judul, a.tanggal_update,a.isi, a.foto, b.nama_kategori FROM artikel as a LEFT JOIN kategori as b on a.id_kategori=b.id_kategori where id_artikel = ' .$id_artikel);
    return $data->result_array();
}
public function get_kategori()
{
    $query = $this->db->get('kategori');
    return $query;
}   
public function UpdateData($tabelNama,$data,$where){
    $res = $this->db->update($tabelNama,$data,$where);
    return $res;
    }

view :

观点:

<html>
<head>
    <title>Data Mahasiswa</title>
</head>
<body>
    <!--<form method="POST" action="<?php echo base_url()."index.php/crud/do_update"; ?>"> -->
        <?php echo form_open_multipart('crud/do_update')?>
   <table>
        <tr>
            <td>Id Artikel</td>
            <td><input type="text" name="id_artikel" value="<?php echo $id_artikel; ?>" readonly/></td>
        </tr>
        <tr>
            <td>Judul</td>
            <td><textarea name="judul"><?php echo $judul; ?></textarea></td>
        </tr>
        <tr>
        <td>Kategori</td>
            <td><label>
            <select name="kategori_terpilih" class="textfield" id="id_kategori">
            <option id="0">--Pilih Kategori--</option>
            <?php
            foreach ($kategori as $a) { ?>
            <option <?php if($kategori_terpilih == $a->id_kategori) echo 'selected = "selected"'; ?> value="<?php echo $a->id_kategori ?>"> <?php echo $a->nama_kategori ?></option>;    
            <?php
            }
            ?>
        </select>  
        </label></td> 
        </tr>
        <tr>
            <td>Isi</td>
            <td><textarea name="isi"><?php echo $isi ?></textarea></td>
        </tr>
        <tr>
            <td>Foto</td>
            <td><input type="file" name="foto" size="20" /><?php echo $foto ?></td>
        </tr>
        <tr>
            <td>
            </td>
            <td><input type="submit" name="btnSubmit" value="Simpan"/></td>
        </tr>

    </table>
    </form>
</body>

1 个解决方案

#1


0  

In your controller, instead of the following line,

在您的控制器中,而不是以下行,

$a['kategori'] = $kategori->result();

Replace with:

用。。。来代替:

$data['kategori'] = $kategori->result();

#1


0  

In your controller, instead of the following line,

在您的控制器中,而不是以下行,

$a['kategori'] = $kategori->result();

Replace with:

用。。。来代替:

$data['kategori'] = $kategori->result();