Create, Read, Update, & Delete Lanjutan – Tutorial CodeIgniter 3

create read update delete

Tutorial crud kali ini kita melanjutkan tutorial crud sebelumnya pada CodeIgniter 3.

Create & Read – Penanaman Modal Dalam Negeri

Buat file view pmdn.php dalam folder pages

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800"><?= $title ?></h1>
        <a href="<?= BASE_URL('investasi/pmdn_create') ?>" class="d-none d-sm-inline-block btn btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Tambah</a>
    </div>

    <div class="row">

        <div class="col-lg-12 mb-4">

            <!-- Illustrations -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Judul</h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <table class="table table-bordered dataTable" id="dataTable" width="100%" cellspacing="0" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
                            <thead>
                                <tr>
                                    <th rowspan="1" colspan="1">Nama</th>
                                    <th rowspan="1" colspan="1">No</th>
                                    <th rowspan="1" colspan="1">Bidang</th>
                                    <th rowspan="1" colspan="1">Alamat</th>
                                    <th rowspan="1" colspan="1">Lokasi</th>
                                    <th rowspan="1" colspan="1">Tambahan</th>
                                    <th rowspan="1" colspan="1">Total</th>
                                    <th rowspan="1" colspan="1">TKI</th>
                                    <th rowspan="1" colspan="1">TKA</th>
                                    <th rowspan="1" colspan="1">AKSI</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($investment as $invest) : ?>
                                    <tr class="odd">
                                        <td class="sorting_1"><?= $invest->nama_perusahaan ?></td>
                                        <td><?= $invest->no_dan_tgl ?></td>
                                        <td><?= $invest->bidang_usaha ?></td>
                                        <td><?= $invest->alamat_kantor ?></td>
                                        <td><?= $invest->nama_wilayah ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_tambahan ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_total ?></td>
                                        <td><?= $invest->tki ?></td>
                                        <td><?= $invest->tka ?></td>
                                        <td>
                                            <a href="#" class="btn btn-sm btn-warning">Ubah</a>
                                            <a href="#" class="btn btn-sm btn-danger">Hapus</a>
                                        </td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- /.container-fluid -->

Buat view untuk menambah investasi pmdn dalam folder pages dengan nama pmdn_create.php

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800"><?= $title ?></h1>
        <!-- <a href="<?= BASE_URL('investasi/pmdn_create') ?>" class="d-none d-sm-inline-block btn btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Tambah</a> -->
    </div>

    <div class="row">

        <div class="col-lg-12 mb-4">

            <!-- Illustrations -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Judul</h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <form method="post" accept-charset="utf-8" action="<?= BASE_URL('investasi/pmdn_create') ?>">

                            <div class="form-group row">
                                <?php echo validation_errors('<button type="button" class="btn btn-outline-danger">', '</button>'); ?>
                            </div>

                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nama perusahaan</label>
                                <div class="col-sm-10">
                                    <input type="text" name="nama_perusahaan" class="form-control" placeholder="nama perusahaan" value="<?= set_value('nama_perusahaan') ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nomor/tanggal</label>
                                <div class="col-sm-10">
                                    <input type="text" name="no_dan_tgl" class="form-control" placeholder="nomor/tanggal" value="<?= set_value('no_dan_tgl') ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">bidang usaha</label>
                                <div class="col-sm-10">
                                    <input type="text" name="bidang_usaha" class="form-control" placeholder="bidang usaha" value="<?= set_value('bidang_usaha') ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">alamat kantor</label>
                                <div class="col-sm-10">
                                    <input type="text" name="alamat_kantor" class="form-control" placeholder="alamat kantor" value="<?= set_value('alamat_kantor') ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Lokasi</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="kode_lokasi" required>
                                        <option value="">-- Pilih wilayah --</option>
                                        <?php foreach ($locations as $location) : ?>
                                            <option value="<?= $location->id ?>" <?= set_select('kode_lokasi', $location->id) ?>><?= $location->nama_lokasi ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">investasi tambahan</label>
                                <div class="col-sm-10">
                                    <input type="text" name="investasi_tambahan" class="form-control" placeholder="investasi tambahan" value="<?= set_value('investasi_tambahan', 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">investasi total</label>
                                <div class="col-sm-10">
                                    <input type="text" name="investasi_total" class="form-control" placeholder="investasi total" value="<?= set_value('investasi_total', 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">tki</label>
                                <div class="col-sm-10">
                                    <input type="text" name="tki" class="form-control" placeholder="tki" value="<?= set_value('tki', 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">tka</label>
                                <div class="col-sm-10">
                                    <input type="text" name="tka" class="form-control" placeholder="tka" value="<?= set_value('tka', 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">mata uang</label>
                                <div class="col-sm-10">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="mata_uang" value="usd" <?= set_radio('mata_uang', 'usd') ?>>
                                        <label class="form-check-label">
                                            USD
                                        </label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="mata_uang" value="rp" <?= set_radio('mata_uang', 'rp', true) ?>>
                                        <label class="form-check-label">
                                            RP
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <input type="submit" value="Simpan" class="btn btn-success">
                                <a href="<?= BASE_URL('investasi/pmdn') ?>" class="btn btn-warning ml-3">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- /.container-fluid -->

Buat file Investasi_model.php dalam folder models
Diawali huruf besar dan diakhiri dengan _model.php

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

class Investasi_model extends CI_Model
{
  public function read($jenisInvestasi)
    {
        $this->db->select('*, tb_investasi.id as id_investasi');
        $this->db->join('tb_negara', 'tb_negara.id = tb_investasi.kode_negara');
        if ($jenisInvestasi == 'pmdn') {
            $this->db->join('tb_lokasi', 'tb_lokasi.id = tb_investasi.kode_lokasi');
            $this->db->where('nama_negara', 'Indonesia');
        } elseif ($jenisInvestasi == 'pma') {
            $this->db->where('nama_negara !=', 'Indonesia');
        }

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

    public function create($data)
    {
        return $this->db->insert('tb_investasi', $data);
    }
}

Buat controller baru dan beri nama Investasi.php
Struktur controllers / Investasi.php

<?php

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

class Investasi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!has_login()) kick_it_out('Halaman yang Anda tuju hanya tersedia untuk pengguna yang sudah login');
        else $this->user = $this->db->get_where('users', ['username' => $this->session->userdata('username')])->row_array();

        $this->load->model('investasi_model');
        $this->load->model('wilayah_model');
    }

    public function pmdn()
    {
        $data['title']      = 'PMDN';
        $data['page']       = 'pages/pmdn';
        $data['user']       = $this->user;
        $data['investment'] = $this->investasi_model->read('pmdn');

        $this->load->view('layouts/app', $data);
    }

    public function pmdn_create()
    {
        $this->form_validation->set_rules('nama_perusahaan', 'Nama perusahaan', 'trim|required');
        $this->form_validation->set_rules('no_dan_tgl', 'Nomor dan tanggal', 'trim|required');
        $this->form_validation->set_rules('bidang_usaha', 'Bidang usaha', 'trim|required');
        $this->form_validation->set_rules('alamat_kantor', 'Alamat kantor', 'trim|required');
        $this->form_validation->set_rules('kode_lokasi', 'Lokasi', 'trim|required|numeric');
        $this->form_validation->set_rules('investasi_tambahan', 'Investasi tambahan', 'trim|numeric');
        $this->form_validation->set_rules('investasi_total', 'Investasi total', 'trim|numeric');
        $this->form_validation->set_rules('tki', 'TKI', 'trim|numeric');
        $this->form_validation->set_rules('tka', 'TKA', 'trim|numeric');
        $this->form_validation->set_rules('mata_uang', 'Mata uang', 'trim|required|in_list[usd,rp]');

        if ($this->form_validation->run() == FALSE) {
            //tampil form
            $data['title']     = 'Input PMDN';
            $data['locations'] = $this->wilayah_model->read('tb_lokasi');
            $data['page']      = 'pages/pmdn_create';
            $data['user']      = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            $data = array(
                'nama_perusahaan' => $this->input->post('nama_perusahaan'),
                'no_dan_tgl' => $this->input->post('no_dan_tgl'),
                'bidang_usaha' => $this->input->post('bidang_usaha'),
                'alamat_kantor' => $this->input->post('alamat_kantor'),
                'kode_lokasi' => $this->input->post('kode_lokasi'),
                'investasi_tambahan' => $this->input->post('investasi_tambahan'),
                'investasi_total' => $this->input->post('investasi_total'),
                'tki' => $this->input->post('tki'),
                'tka' => $this->input->post('tka'),
                'mata_uang' => $this->input->post('mata_uang')
            );

            if ($this->investasi_model->create($data)) {
                return redirect(base_url('investasi/pmdn'));
            } else {
                echo 'terjadi kesalahan';
            }
        }
    }
}

Ubah _sidebar.php yang ada dalam view layouts
Buat link menuju admin / pmdn /

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

function nav_item($segment)
{
    $ci = get_instance();
    if ($segment == $ci->uri->segment(2)) {
        $nav_item = 'nav-item active';
    } else {
        $nav_item = 'nav-item';
    }

    return $nav_item;
}

?>

<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

    <!-- Sidebar - Brand -->
    <a class="sidebar-brand d-flex align-items-center justify-content-center" href="<?= base_url() ?>">
        <div class="sidebar-brand-icon rotate-n-15">
            <i class="fas fa-home"></i>
        </div>
        <div class="sidebar-brand-text mx-3">Belajar CI3</div>
    </a>

    <!-- Divider -->
    <hr class="sidebar-divider">

    <!-- Nav Item - Dashboard -->
    <li class="<?= nav_item('welcome') ?>">
        <a class="nav-link" href="<?= BASE_URL('admin') ?>">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Dasbor</span>
        </a>
    </li>

    <!-- Divider -->
    <hr class="sidebar-divider">

    <li class="<?= nav_item('wilayah') ?>">
        <a class="nav-link" href="<?= BASE_URL('wilayah') ?>">
            <i class="fas fa-fw fa-map"></i>
            <span>Wilayah</span>
        </a>
    </li>

    <!-- Nav Item - Template Collapse Menu -->
    <li class="<?= nav_item('investasi') ?>">
        <a class="nav-link pb-0 collapsed" href="#" data-toggle="collapse" data-target="#collapseData" aria-expanded="true" aria-controls="collapseData">
            <i class="fas fa-fw fa-bezier-curve"></i>
            <span>Investasi</span>
        </a>
        <div id="collapseData" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <h6 class="collapse-header">Menu Investasi:</h6>
                <a class="collapse-item" href="<?= BASE_URL('investasi/pmdn') ?>">PMDN</a>
                <a class="collapse-item" href="#">PMA</a>
            </div>
        </div>
    </li>


    <!-- Divider -->
    <hr class="sidebar-divider mt-3">

    <!-- Nav Item - Tables -->
    <li class="<?= nav_item('logout') ?>">
        <a class="nav-link pb-0" data-toggle="modal" data-target="#logoutModal" href="<?= BASE_URL('auth/logout') ?>">
            <i class="fas fa-fw fa-door-open"></i>
            <span>Logout</span>
        </a>
    </li>

    <!-- Divider -->
    <hr class="sidebar-divider mt-3 d-none d-md-block">

    <!-- Sidebar Toggler (Sidebar) -->
    <div class="text-center d-none d-md-inline">
        <button class="rounded-circle border-0" id="sidebarToggle"></button>
    </div>

</ul>
<!-- End of Sidebar -->

Sekarang silakan akses halaman investasi pmdn dan coba input data pmdn

Update – Penanaman Modal Dalam Negeri

Tambahkan method update pada controller Investasi.php

<?php

defined('BASEPATH') or exit('No direct script access allowed');
//create update delete bisa digabung karena memiliki 1 tabel
class Investasi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!has_login()) kick_it_out('Halaman yang Anda tuju hanya tersedia untuk pengguna yang sudah login');
        else $this->user = $this->db->get_where('users', ['username' => $this->session->userdata('username')])->row_array();

        $this->load->model('investasi_model');
        $this->load->model('wilayah_model');
    }

    public function pmdn()
    {
        $data['title']      = 'PMDN';
        $data['page']       = 'pages/pmdn';
        $data['user']       = $this->user;
        $data['investment'] = $this->investasi_model->read('pmdn');

        $this->load->view('layouts/app', $data);
    }

    public function pmdn_create()
    {
        $this->form_validation->set_rules('nama_perusahaan', 'Nama perusahaan', 'trim|required');
        $this->form_validation->set_rules('no_dan_tgl', 'Nomor dan tanggal', 'trim|required');
        $this->form_validation->set_rules('bidang_usaha', 'Bidang usaha', 'trim|required');
        $this->form_validation->set_rules('alamat_kantor', 'Alamat kantor', 'trim|required');
        $this->form_validation->set_rules('kode_lokasi', 'Lokasi', 'trim|required|numeric');
        $this->form_validation->set_rules('investasi_tambahan', 'Investasi tambahan', 'trim|numeric');
        $this->form_validation->set_rules('investasi_total', 'Investasi total', 'trim|numeric');
        $this->form_validation->set_rules('tki', 'TKI', 'trim|numeric');
        $this->form_validation->set_rules('tka', 'TKA', 'trim|numeric');
        $this->form_validation->set_rules('mata_uang', 'Mata uang', 'trim|required|in_list[usd,rp]');

        if ($this->form_validation->run() == FALSE) {
            //tampil form
            $data['title']     = 'Input PMDN';
            $data['locations'] = $this->wilayah_model->read('tb_lokasi');
            $data['page']      = 'pages/pmdn_create';
            $data['user']      = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            $data = array(
                'nama_perusahaan'    => $this->input->post('nama_perusahaan'),
                'no_dan_tgl'         => $this->input->post('no_dan_tgl'),
                'bidang_usaha'       => $this->input->post('bidang_usaha'),
                'alamat_kantor'      => $this->input->post('alamat_kantor'),
                'kode_lokasi'        => $this->input->post('kode_lokasi'),
                'investasi_tambahan' => $this->input->post('investasi_tambahan'),
                'investasi_total'    => $this->input->post('investasi_total'),
                'tki'                => $this->input->post('tki'),
                'tka'                => $this->input->post('tka'),
                'mata_uang'          => $this->input->post('mata_uang')
            );

            if ($this->investasi_model->create($data)) {
                return redirect(base_url('investasi/pmdn'));
            } else {
                echo 'terjadi kesalahan';
            }
        }
    }

    public function pmdn_update($id)
    {
        $this->form_validation->set_rules('nama_perusahaan', 'Nama perusahaan', 'trim|required');
        $this->form_validation->set_rules('no_dan_tgl', 'Nomor dan tanggal', 'trim|required');
        $this->form_validation->set_rules('bidang_usaha', 'Bidang usaha', 'trim|required');
        $this->form_validation->set_rules('alamat_kantor', 'Alamat kantor', 'trim|required');
        $this->form_validation->set_rules('kode_lokasi', 'Lokasi', 'trim|required|numeric');
        $this->form_validation->set_rules('investasi_tambahan', 'Investasi tambahan', 'trim|numeric');
        $this->form_validation->set_rules('investasi_total', 'Investasi total', 'trim|numeric');
        $this->form_validation->set_rules('tki', 'TKI', 'trim|numeric');
        $this->form_validation->set_rules('tka', 'TKA', 'trim|numeric');
        $this->form_validation->set_rules('mata_uang', 'Mata uang', 'trim|required|in_list[usd,rp]');

        if ($this->form_validation->run() == FALSE) {
            //tampil form
            $data['title']      = 'Update PMDN';
            $data['locations']  = $this->wilayah_model->read('tb_lokasi');
            $data['page']       = 'pages/pmdn_update';
            $data['user']       = $this->user;
            $data['investment'] = $this->investasi_model->readById($id);

            $this->load->view('layouts/app', $data);
        } else {
            $data = array(
                'id'                 => $id,
                'nama_perusahaan'    => $this->input->post('nama_perusahaan'),
                'no_dan_tgl'         => $this->input->post('no_dan_tgl'),
                'bidang_usaha'       => $this->input->post('bidang_usaha'),
                'alamat_kantor'      => $this->input->post('alamat_kantor'),
                'kode_lokasi'        => $this->input->post('kode_lokasi'),
                'investasi_tambahan' => $this->input->post('investasi_tambahan'),
                'investasi_total'    => $this->input->post('investasi_total'),
                'tki'                => $this->input->post('tki'),
                'tka'                => $this->input->post('tka'),
                'mata_uang'          => $this->input->post('mata_uang')
            );

            if ($this->investasi_model->update($data)) {
                return redirect(base_url('investasi/pmdn'));
            } else {
                echo 'terjadi kesalahan';
            }
        }
    }
}

Tambahkan method readById & method update pada Investasi_model.php

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

class Investasi_model extends CI_Model
{
    public function read($jenisInvestasi)
    {
        $this->db->select('*, tb_investasi.id as id_investasi');
        $this->db->join('tb_lokasi', 'tb_lokasi.id = tb_investasi.kode_lokasi');
        $this->db->join('tb_negara', 'tb_negara.id = tb_investasi.kode_negara');
        if ($jenisInvestasi == 'pmdn') {
            $this->db->where('nama_negara', 'Indonesia');
        } elseif ($jenisInvestasi == 'pma') {
            $this->db->where('nama_negara !=', 'Indonesia');
        }

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

    public function create($data)
    {
        return $this->db->insert('tb_investasi', $data);
    }

    public function readById($id)
    {
        $this->db->select('*, tb_investasi.id as id_investasi');
        $this->db->join('tb_lokasi', 'tb_lokasi.id = tb_investasi.kode_lokasi');
        $this->db->join('tb_negara', 'tb_negara.id = tb_investasi.kode_negara');

        return $this->db->where('tb_investasi.id', $id)->get('tb_investasi')->row_object();
    }

    public function update($data)
    {
        return $this->db->update('tb_investasi', $data, ['id' => $data['id']]);
    }
}

Ubah link tujuan tomboh ubah pada view pages/pmdn.php

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800"><?= $title ?></h1>
        <a href="<?= BASE_URL('investasi/pmdn_create') ?>" class="d-none d-sm-inline-block btn btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Tambah</a>
    </div>

    <div class="row">

        <div class="col-lg-12 mb-4">

            <!-- Illustrations -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Judul</h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <table class="table table-bordered dataTable" id="dataTable" width="100%" cellspacing="0" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
                            <thead>
                                <tr>
                                    <th rowspan="1" colspan="1">Nama</th>
                                    <th rowspan="1" colspan="1">No</th>
                                    <th rowspan="1" colspan="1">Bidang</th>
                                    <th rowspan="1" colspan="1">Alamat</th>
                                    <th rowspan="1" colspan="1">Lokasi</th>
                                    <th rowspan="1" colspan="1">Tambahan</th>
                                    <th rowspan="1" colspan="1">Total</th>
                                    <th rowspan="1" colspan="1">TKI</th>
                                    <th rowspan="1" colspan="1">TKA</th>
                                    <th rowspan="1" colspan="1">AKSI</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($investment as $invest) : ?>
                                    <tr class="odd">
                                        <td class="sorting_1"><?= $invest->nama_perusahaan ?></td>
                                        <td><?= $invest->no_dan_tgl ?></td>
                                        <td><?= $invest->bidang_usaha ?></td>
                                        <td><?= $invest->alamat_kantor ?></td>
                                        <td><?= $invest->nama_lokasi ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_tambahan ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_total ?></td>
                                        <td><?= $invest->tki ?></td>
                                        <td><?= $invest->tka ?></td>
                                        <td>
                                            <a href="<?= BASE_URL('investasi/pmdn_update/' . $invest->id_investasi) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                            <a href="#" class="btn btn-sm btn-danger">Hapus</a>
                                        </td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- /.container-fluid -->

Tambah view baru dalam pages untuk halaman mengedit PMDN beri nama pmdn_update.php

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800"><?= $title ?></h1>
        <!-- <a href="<?= BASE_URL('investasi/pmdn_create') ?>" class="d-none d-sm-inline-block btn btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Tambah</a> -->
    </div>

    <div class="row">

        <div class="col-lg-12 mb-4">

            <!-- Illustrations -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Judul</h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <form method="post" accept-charset="utf-8" action="<?= BASE_URL('investasi/pmdn_update/' . $investment->id_investasi) ?>">

                            <div class="form-group row">
                                <?php echo validation_errors('<button type="button" class="btn btn-outline-danger">', '</button>'); ?>
                            </div>

                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nama perusahaan</label>
                                <div class="col-sm-10">
                                    <input type="text" name="nama_perusahaan" class="form-control" placeholder="nama perusahaan" value="<?= set_value('nama_perusahaan', $investment->nama_perusahaan) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nomor/tanggal</label>
                                <div class="col-sm-10">
                                    <input type="text" name="no_dan_tgl" class="form-control" placeholder="nomor/tanggal" value="<?= set_value('no_dan_tgl', $investment->no_dan_tgl) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">bidang usaha</label>
                                <div class="col-sm-10">
                                    <input type="text" name="bidang_usaha" class="form-control" placeholder="bidang usaha" value="<?= set_value('bidang_usaha', $investment->bidang_usaha) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">alamat kantor</label>
                                <div class="col-sm-10">
                                    <input type="text" name="alamat_kantor" class="form-control" placeholder="alamat kantor" value="<?= set_value('alamat_kantor', $investment->alamat_kantor) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Lokasi</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="kode_lokasi">
                                        <?php foreach ($locations as $location) : ?>
                                            <?php $location->id == $investment->kode_lokasi ? $selected = 'selected' : $selected = ''; ?>
                                            <option <?= set_select('kode_lokasi') ?> value="<?= $location->id ?>" <?= $selected; ?>><?= $location->nama_lokasi ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">investasi tambahan</label>
                                <div class="col-sm-10">
                                    <input type="text" name="investasi_tambahan" class="form-control" placeholder="investasi tambahan" value="<?= set_value('investasi_tambahan', $investment->investasi_tambahan, 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">investasi total</label>
                                <div class="col-sm-10">
                                    <input type="text" name="investasi_total" class="form-control" placeholder="investasi total" value="<?= set_value('investasi_total', $investment->investasi_total, 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">tki</label>
                                <div class="col-sm-10">
                                    <input type="text" name="tki" class="form-control" placeholder="tki" value="<?= set_value('tki', $investment->tki, 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">tka</label>
                                <div class="col-sm-10">
                                    <input type="text" name="tka" class="form-control" placeholder="tka" value="<?= set_value('tka', $investment->tka, 0) ?>">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">mata uang</label>
                                <div class="col-sm-10">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="mata_uang" value="usd" <?= set_radio('mata_uang', 'usd', $investment->mata_uang == 'usd') ?>>
                                        <label class="form-check-label">
                                            USD
                                        </label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="mata_uang" value="rp" <?= set_radio('mata_uang', 'rp', $investment->mata_uang == 'rp') ?>>
                                        <label class="form-check-label">
                                            RP
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <input type="submit" value="Simpan" class="btn btn-success">
                                <a href="<?= BASE_URL('investasi/pmdn') ?>" class="btn btn-warning ml-3">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- /.container-fluid -->

Sekarang silakan akses halaman investasi pmdn dan coba ubah salah satu data pmdn

Delete – Penanaman Modal Dalam Negeri

Tambahkan method delete pada controller Investasi.php

<?php

defined('BASEPATH') or exit('No direct script access allowed');
//create update delete bisa digabung karena memiliki 1 tabel
class Investasi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!has_login()) kick_it_out('Halaman yang Anda tuju hanya tersedia untuk pengguna yang sudah login');
        else $this->user = $this->db->get_where('users', ['username' => $this->session->userdata('username')])->row_array();

        $this->load->model('investasi_model');
        $this->load->model('wilayah_model');
    }

    public function pmdn()
    {
        $data['title']      = 'PMDN';
        $data['page']       = 'pages/pmdn';
        $data['user']       = $this->user;
        $data['investment'] = $this->investasi_model->read('pmdn');

        $this->load->view('layouts/app', $data);
    }

    public function pmdn_create()
    {
        $this->form_validation->set_rules('nama_perusahaan', 'Nama perusahaan', 'trim|required');
        $this->form_validation->set_rules('no_dan_tgl', 'Nomor dan tanggal', 'trim|required');
        $this->form_validation->set_rules('bidang_usaha', 'Bidang usaha', 'trim|required');
        $this->form_validation->set_rules('alamat_kantor', 'Alamat kantor', 'trim|required');
        $this->form_validation->set_rules('kode_lokasi', 'Lokasi', 'trim|required|numeric');
        $this->form_validation->set_rules('investasi_tambahan', 'Investasi tambahan', 'trim|numeric');
        $this->form_validation->set_rules('investasi_total', 'Investasi total', 'trim|numeric');
        $this->form_validation->set_rules('tki', 'TKI', 'trim|numeric');
        $this->form_validation->set_rules('tka', 'TKA', 'trim|numeric');
        $this->form_validation->set_rules('mata_uang', 'Mata uang', 'trim|required|in_list[usd,rp]');

        if ($this->form_validation->run() == FALSE) {
            //tampil form
            $data['title']     = 'Input PMDN';
            $data['locations'] = $this->wilayah_model->read('tb_lokasi');
            $data['page']      = 'pages/pmdn_create';
            $data['user']      = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            $data = array(
                'nama_perusahaan'    => $this->input->post('nama_perusahaan'),
                'no_dan_tgl'         => $this->input->post('no_dan_tgl'),
                'bidang_usaha'       => $this->input->post('bidang_usaha'),
                'alamat_kantor'      => $this->input->post('alamat_kantor'),
                'kode_lokasi'        => $this->input->post('kode_lokasi'),
                'investasi_tambahan' => $this->input->post('investasi_tambahan'),
                'investasi_total'    => $this->input->post('investasi_total'),
                'tki'                => $this->input->post('tki'),
                'tka'                => $this->input->post('tka'),
                'mata_uang'          => $this->input->post('mata_uang')
            );

            if ($this->investasi_model->create($data)) {
                return redirect(base_url('investasi/pmdn'));
            } else {
                echo 'terjadi kesalahan';
            }
        }
    }

    public function pmdn_update($id)
    {
        $this->form_validation->set_rules('nama_perusahaan', 'Nama perusahaan', 'trim|required');
        $this->form_validation->set_rules('no_dan_tgl', 'Nomor dan tanggal', 'trim|required');
        $this->form_validation->set_rules('bidang_usaha', 'Bidang usaha', 'trim|required');
        $this->form_validation->set_rules('alamat_kantor', 'Alamat kantor', 'trim|required');
        $this->form_validation->set_rules('kode_lokasi', 'Lokasi', 'trim|required|numeric');
        $this->form_validation->set_rules('investasi_tambahan', 'Investasi tambahan', 'trim|numeric');
        $this->form_validation->set_rules('investasi_total', 'Investasi total', 'trim|numeric');
        $this->form_validation->set_rules('tki', 'TKI', 'trim|numeric');
        $this->form_validation->set_rules('tka', 'TKA', 'trim|numeric');
        $this->form_validation->set_rules('mata_uang', 'Mata uang', 'trim|required|in_list[usd,rp]');

        if ($this->form_validation->run() == FALSE) {
            //tampil form
            $data['title']      = 'Update PMDN';
            $data['locations']  = $this->wilayah_model->read('tb_lokasi');
            $data['page']       = 'pages/pmdn_update';
            $data['user']       = $this->user;
            $data['investment'] = $this->investasi_model->readById($id);

            $this->load->view('layouts/app', $data);
        } else {
            $data = array(
                'id'                 => $id,
                'nama_perusahaan'    => $this->input->post('nama_perusahaan'),
                'no_dan_tgl'         => $this->input->post('no_dan_tgl'),
                'bidang_usaha'       => $this->input->post('bidang_usaha'),
                'alamat_kantor'      => $this->input->post('alamat_kantor'),
                'kode_lokasi'        => $this->input->post('kode_lokasi'),
                'investasi_tambahan' => $this->input->post('investasi_tambahan'),
                'investasi_total'    => $this->input->post('investasi_total'),
                'tki'                => $this->input->post('tki'),
                'tka'                => $this->input->post('tka'),
                'mata_uang'          => $this->input->post('mata_uang')
            );

            if ($this->investasi_model->update($data)) {
                return redirect(base_url('investasi/pmdn'));
            } else {
                echo 'terjadi kesalahan';
            }
        }
    }

    public function delete($jenis, $id)
    {
        if ($this->investasi_model->delete($id)) {
            return redirect(base_url('investasi/' . $jenis));
        }
    }
}

Tambahkan juga method delete pada model Investasi_model.php

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

class Investasi_model extends CI_Model
{
    public function read($jenisInvestasi)
    {
        $this->db->select('*, tb_investasi.id as id_investasi');
        $this->db->join('tb_lokasi', 'tb_lokasi.id = tb_investasi.kode_lokasi');
        $this->db->join('tb_negara', 'tb_negara.id = tb_investasi.kode_negara');
        if ($jenisInvestasi == 'pmdn') {
            $this->db->where('nama_negara', 'Indonesia');
        } elseif ($jenisInvestasi == 'pma') {
            $this->db->where('nama_negara !=', 'Indonesia');
        }

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

    public function create($data)
    {
        return $this->db->insert('tb_investasi', $data);
    }

    public function readById($id)
    {
        $this->db->select('*, tb_investasi.id as id_investasi');
        $this->db->join('tb_lokasi', 'tb_lokasi.id = tb_investasi.kode_lokasi');
        $this->db->join('tb_negara', 'tb_negara.id = tb_investasi.kode_negara');

        return $this->db->where('tb_investasi.id', $id)->get('tb_investasi')->row_object();
    }

    public function update($data)
    {
        return $this->db->update('tb_investasi', $data, ['id' => $data['id']]);
    }

    public function delete($id)
    {
        return $this->db->delete('tb_investasi', array('id' => $id));
    }
}

Ubah tombol hapus pada view pages/pmdn.php dan arahkan ke method delete pada controller Investasi.php tadi

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800"><?= $title ?></h1>
        <a href="<?= BASE_URL('investasi/pmdn_create') ?>" class="d-none d-sm-inline-block btn btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Tambah</a>
    </div>

    <div class="row">

        <div class="col-lg-12 mb-4">

            <!-- Illustrations -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Judul</h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <table class="table table-bordered dataTable" id="dataTable" width="100%" cellspacing="0" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
                            <thead>
                                <tr>
                                    <th rowspan="1" colspan="1">Nama</th>
                                    <th rowspan="1" colspan="1">No</th>
                                    <th rowspan="1" colspan="1">Bidang</th>
                                    <th rowspan="1" colspan="1">Alamat</th>
                                    <th rowspan="1" colspan="1">Lokasi</th>
                                    <th rowspan="1" colspan="1">Tambahan</th>
                                    <th rowspan="1" colspan="1">Total</th>
                                    <th rowspan="1" colspan="1">TKI</th>
                                    <th rowspan="1" colspan="1">TKA</th>
                                    <th rowspan="1" colspan="1">AKSI</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($investment as $invest) : ?>
                                    <tr class="odd">
                                        <td class="sorting_1"><?= $invest->nama_perusahaan ?></td>
                                        <td><?= $invest->no_dan_tgl ?></td>
                                        <td><?= $invest->bidang_usaha ?></td>
                                        <td><?= $invest->alamat_kantor ?></td>
                                        <td><?= $invest->nama_lokasi ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_tambahan ?></td>
                                        <td><?= ucwords($invest->mata_uang) ?> <?= $invest->investasi_total ?></td>
                                        <td><?= $invest->tki ?></td>
                                        <td><?= $invest->tka ?></td>
                                        <td>
                                            <a href="<?= BASE_URL('investasi/pmdn_update/' . $invest->id_investasi) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                            <a href="<?= BASE_URL('investasi/delete/pmdn/' . $invest->id_investasi) ?>" class="btn btn-sm btn-danger">Hapus</a>
                                        </td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- /.container-fluid -->

Sekarang silakan akses halaman investasi pmdn dan coba hapus salah satu data pmdn

About the Author

Assalamualaikum. Saya Ahmad Mazlan, Programmer muda yang masih belajar dan ingin segera berbagi tentang dunia programming. Siapa pun bisa membuat program, yang mereka butuhkan hanya belajar. Semua bisa menjadi Programmer selama memiliki keinginan yang kuat untuk belajar.

Tinggalkan Balasan