cara menampilkan data dari tabel database yang berrelasi ke dalam form select option di ci

assalamuailkum, salam sejahtera saya memiliki kendala menampilkan data dari tabel database kategori ke dalam form select option input produk dengan menggunakan CI berikut source code dan gambarnya:

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Kategori extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('Model_kategori');
        chek_session();
    }
    public function index()
    {

        $data['data']= $this->Model_kategori->tampilkan_data()->result();
        //$query['data'] = $this->db->get('kategori_barang')->result();

        $this->template->load('template', 'kategori/view_kategori', $data);
        $this->load->view('kategori/js/index');
    }

    public function addData()
    {
        $data = array('nama_kategori' => $this->input->post('name_kategori'));
        // var_dump($data);
        //$this->load->model('Model_kategori');
        $this->Model_kategori->add_kategori($data);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to add!</strong>
      </div>');
        redirect("Kategori/index");
    }

    public function edit($id)
    {
        //$this->load->model('Model_kategori');
        $query['data'] = $this->Model_kategori->edit_kategori($id);
        header('Content-Type: application/json');
        // $this->load->view('kategori/edit_kategori', $query);
        // $this->load->json($query);
        echo json_encode($query);
    }
    public function editSave($id)
    {
        $whare = array('kategori_id' => $id);
        $data = [
            'nama_kategori' => $this->input->post('name_kategori_edit')
        ];
        //$this->load->model('Model_kategori');
        $this->Model_kategori->edit_save_kategori($whare, $data);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to update!</strong>
      </div>');
        redirect("Kategori/index");
    }

    public function delete($id)
    {
        $whare = array('kategori_id' => $id);
        //$this->load->model('Model_kategori');
        $this->Model_kategori->hapus_kategori($whare);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to deleted!</strong>
      </div>');
        redirect("Kategori/index");
    }
}

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Produk extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('Model_produk');
        chek_session();
    }
    public function index()
    {

        //$query['data'] = $this->db->get('tbl_produk')->result();

        $data['data']   = $this->Model_produk->tampil_data()->result();

        $this->template->load('template', 'produk/view_produk', $data);
        $this->load->view('produk/js/index');
    }

    public function addData()
    {
        $config['upload_path'] = './assets/images/'; //path folder
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload

        $this->upload->initialize($config);
        if(!empty($_FILES['filefoto']['name'])){

            if ($this->upload->do_upload('filefoto')){
                $gbr = $this->upload->data();
                //Compress Image
                $config['image_library']='gd2';
                $config['source_image']='./assets/images/'.$gbr['file_name'];
                $config['create_thumb']= FALSE;
                $config['maintain_ratio']= FALSE;
                $config['quality']= '50%';
                $config['width']= 600;
                $config['height']= 400;
                $config['new_image']= './assets/images/'.$gbr['file_name'];
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();

                $gambar=$gbr['file_name'];
                $foto = $this->upload->data('file_name');
            }

        }else{
            echo "Image yang diupload kosong";
        }


        $data = array(
            'id_kategori'       => $this->input->post('name_kategori'),
            'nama_produk'       => $this->input->post('name_produk'),
            'kode_produk'       => $this->input->post('kode_produk'),
            'foto_produk'       => $foto,
            'tgl_register'      => $this->input->post('tgl_register')
        );

        $this->load->model('Model_kategori');
        $data['name_kategori']=  $this->Model_kategori->tampilkan_data()->result();


        //$data = array('nama_kategori' => $this->input->post('name_kategori'));
        // var_dump($data);
        //$this->load->model('Model_kategori');
        $this->Model_produk->add_kategori($data);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to add!</strong>
      </div>');
        redirect("Produk/index");

    }

    public function edit($id)
    {
        //$this->load->model('Model_Produk');
        $query['data'] = $this->Model_produk->edit_kategori($id);
        header('Content-Type: application/json');
        // $this->load->view('kategori/edit_kategori', $query);
        // $this->load->json($query);
        echo json_encode($query);
    }
    public function editSave($id)
    {
        $whare = array('id_produk' => $id);
        $data = [
            'nama_produk' => $this->input->post('name_kategori_edit')
        ];
        //$this->load->model('Model_produk');
        $this->Model_produk->edit_save_kategori($whare, $data);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to update!</strong>
      </div>');
        redirect("Produk/index");
    }

    public function delete($id)
    {
        $whare = array('id_produk' => $id);
        //$this->load->model('Model_produk');
        $this->Model_produk->hapus_kategori($whare);

        $this->session->set_flashdata('massage', '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success to deleted!</strong>
      </div>');
        redirect("Produk/index");
    }
}

model

<?php
class Model_kategori extends CI_Model
{

    function tampilkan_data(){

        return $this->db->get('kategori_barang');
    }

    public function add_kategori($data)
    {
        $this->db->insert('kategori_barang', $data);
    }
    public function edit_kategori($id)
    {
        $whare = array('kategori_id' => $id);
        return $this->db->get_where('kategori_barang', $whare)->result();
    }

    public function edit_save_kategori($whare, $data)
    {
        $this->db->where($whare);
        return $this->db->update('kategori_barang', $data);
    }

    public function hapus_kategori($whare)
    {
        $this->db->where($whare);
        $this->db->delete('kategori_barang');
    }

}
<?php
class Model_produk extends CI_Model
{

    function tampil_data()
    {
        $query= "SELECT b.id_produk,b.nama_produk,kb.nama_kategori,b.kode_produk,b.foto_produk,
                b.tgl_register
                FROM tbl_produk as b,kategori_barang as kb
                WHERE b.id_kategori=kb.kategori_id";
        return $this->db->query($query);
    }
    public function add_kategori($data)
    {
        $this->db->insert('tbl_produk', $data);
    }
    public function edit_kategori($id)
    {
        $whare = array('id_produk' => $id);
        return $this->db->get_where('tbl_produk', $whare)->result();
    }

    public function edit_save_kategori($whare, $data)
    {
        $this->db->where($whare);
        return $this->db->update('tbl_produk', $data);
    }

    public function hapus_kategori($whare)
    {
        $this->db->where($whare);
        $this->db->delete('tbl_produk');
    }

}

view

<?= $this->session->flashdata('massage'); ?>
<section class="mb-3">
    <div class="container">
        <div class="add-data mb-2">
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                Add Katagory
            </button>
        </div>
        <div class="data-product mb-2">
            <table id="example" class="table table-bordered " style="width:100%">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Nama Kategori</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $no = 1;
                    foreach ($data as $kategori_data) : ?>
                        <tr>
                            <td><?= $no++; ?></td>
                            <td><?= $kategori_data->nama_kategori; ?></td>
                            <td>
                                <div class="action">
                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#popupEdit" onclick="getEdit(<?= $kategori_data->kategori_id; ?>)">
                                        Edit Kategory
                                    </button>

                                    <a href="<?= base_url('Kategori/delete/') .$kategori_data->kategori_id; ?>" class="btn btn-danger" onclick="javascript: return confirm('anda yakin?')">Delete</a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                </tbody>
            </table>
        </div>

    </div>
</section>

<?php  //var_dump($data);
?>


<!-- Modal add-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <form action="<?= base_url('Kategori/addData'); ?>" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="name_kategori">Nama Kategori</label>
                        <input type="text" class="form-control" name="name_kategori" id="name_kategori" placeholder="Please Insert">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-default">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- AKhir modal add -->


<!-- modal edit -->
<div class="modal fade" id="popupEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <form id="editSave" action="" method="post">
                <div class="modal-body">
                <div class="form-group">
                        <label for="name_kategori_edit">Nama Kategori</label>
                        <input type="text" class="form-control" name="name_kategori_edit" id="name_kategori_edit" placeholder="Please Insert" value="">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save changes</button>
                </div>
            </form>
        </div>
    </div>
</div> <!-- AKhir modal edit -->
<?= $this->session->flashdata('massage'); ?>
<section class="mb-3">
    <div class="container">
        <div class="add-data mb-2">
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                Add Katagory
            </button>
        </div>
        <div class="data-product mb-2">
            <table id="example" class="table table-bordered " style="width:100%">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>ID Produk</th>
                        <th>Nama Kategori</th>
                        <th>Nama Produk</th>
                        <th>Kode Produk</th>
                        <th>Foto Produk</th>
                        <th>Tanggal Register</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $no = 1;
                    foreach ($data as $kategori_data) : ?>
                        <tr>
                            <td><?= $no++; ?></td>
                            <td><?= $kategori_data->id_produk; ?></td>
                            <td><?= $kategori_data->nama_kategori; ?></td>
                            <td><?= $kategori_data->nama_produk; ?></td>
                            <td><?= $kategori_data->kode_produk; ?></td>
                            <td><?= $kategori_data->foto_produk; ?></td>
                            <td><?= $kategori_data->tgl_register; ?></td>
                            <td>
                                <div class="action">
                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#popupEdit" onclick="getEdit(<?= $kategori_data->id_produk; ?>)">
                                        Edit
                                    </button>

                                    <a href="<?= base_url('Produk/delete/') .$kategori_data->id_produk; ?>" class="btn btn-danger" onclick="javascript: return confirm('anda yakin?')">Delete</a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                </tbody>
            </table>
        </div>

    </div>
</section>

<?php  //var_dump($data);
?>


<!-- Modal add-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Add Produk</h4>
            </div>
            <form action="<?= base_url('Produk/addData'); ?>" method="post">

                <div class="modal-body">

                        <div class="form-group">
                                <div class="row">
                                    <label for="name_kategori" class="col-sm-4 control-label">Nama Kategori</label>
                                    <label class="col-sm-1 control-label">: </label>
                                    <div class="col-sm-7">
                                        <select class="form-control" id="name_kategori" name="name_kategori">
                                        <?php
                                        foreach ($kategori as $row) {

                                            echo "<option value='$k->kategori_id'>".$k->nama_kategori."</option>";
                                        }
                                        ?>
                                        <!--<option value="Pcs">Pcs</option>-->
                                        </select>
                                        <!--<input type="text" class="form-control" name="name_kategori" id="name_kategori" placeholder="Nama Kategori">-->
                                    </div>
                                </div>
                        </div> <!-- /form-group-->

                        <div class="form-group">
                                <div class="row">
                                    <label for="name_produk" class="col-sm-4 control-label">Nama Produk</label>
                                    <label class="col-sm-1 control-label">: </label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="name_produk" id="name_produk" placeholder="Nama Produk">
                                    </div>
                                </div>
                        </div> <!-- /form-group-->

                        <div class="form-group">
                                <div class="row">
                                    <label for="kode_produk" class="col-sm-4 control-label">Kode Produk</label>
                                    <label class="col-sm-1 control-label">: </label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="kode_produk" id="kode_produk" placeholder="Kode Produk">
                                    </div>
                                </div>
                        </div> <!-- /form-group-->

                        <div class="form-group">
                                <div class="row">
                                    <label for="photo" class="col-sm-4 control-label">Upload Foto</label>
                                    <label class="col-sm-1 control-label">: </label>
                                    <div class="col-sm-7">
                                            <input name="photo" id="photo" type="file">
                                            <span class="help-block"></span>
                                    </div>
                                </div>
                        </div> <!-- /form-group-->

                        <div class="form-group ">
                                <div class="row">
                                    <label for="tgl_register" class="col-sm-4 control-label">Tanggal Register</label>
                                    <label class="col-sm-1 control-label">: </label>
                                    <div class="col-sm-7">
                                    <input data-date="" data-date-format="yyyy-mm-dd" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd" name="tgl_register" placeholder="yyyy-mm-dd" class="form-control datepicker" type="text">
                                    </div>
                                </div>
                        </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- AKhir modal add -->

<!-- modal edit -->
<!-- modal edit -->
<div class="modal fade" id="popupEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <form id="editSave" action="" method="post">
                <div class="modal-body">
                <div class="form-group">
                        <label for="name_kategori_edit">Nama Kategori</label>
                        <input type="text" class="form-control" name="name_kategori_edit" id="name_kategori_edit" placeholder="Please Insert" value="">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save changes</button>
                </div>
            </form>
        </div>
    </div>
</div> <!-- AKhir modal edit -->

https://imgur.com/a/RBXBH0i  ini gambar form input produk dan tabel kategori

avatar 4hm4d
@4hm4d

5 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban