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

Mempelajari suatu bahasa atau framework biasa diawali dengan ng-print sesuatu seperti Hello world! kemudian setelah itu target saya dan ini juga merupakan pengalaman beberapa teman programmer lainnya adalah dengan mempelajari Create, Read, Update, & Delete, empat hal inilah yang akan menjadi dasar kita dalam membangun aplikasi apapun ke depannya.

Create & Read – Wilayah

Buat Wilayah_model.php untuk berinteraksi dengan database

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

class Wilayah_model extends CI_Model
{
    public function read(String $tabel)
    {
        if ($tabel == 'tb_negara') {
            return $this->db->where('nama_negara !=', 'Indonesia')->get($tabel)->result();
        }
        return $this->db->get($tabel)->result();
    }

    public function create($jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
            $data = [
                'nama_lokasi' => $this->input->post('nama_wilayah')
            ];
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
            $data = [
                'nama_negara' => $this->input->post('nama_wilayah')
            ];
        }

        return $this->db->insert($tabel, $data);
    }
}

Buat view untuk melihat daftar wilayah (read) dalam pages dengan nama wilayah.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('wilayah/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">Kelola Wilayah</h6>
                </div>

                <div class="row">

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/lokasi') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Lokasi</a>
                            <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 Lokasi</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($lokasi as $location) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $location->nama_lokasi ?></td>
                                            <td>
                                                <a href="#" class="btn btn-sm btn-warning">Aksi 1</a>
                                                <a href="#" class="btn btn-sm btn-danger">Aksi 2</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/negara') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Negara</a>
                            <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 Negara</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($negara as $country) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $country->nama_negara ?></td>
                                            <td>
                                                <a href="#" class="btn btn-sm btn-warning">Aksi 1</a>
                                                <a href="#" class="btn btn-sm btn-danger">Aksi 2</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>

            </div>

        </div>

    </div>

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

Buat view 1 lagi dalam pages untuk menginput wilayah (create) beri nama wilayah_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"><?= ucwords($jenis) ?></h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <form method="post" accept-charset="utf-8" action="<?= BASE_URL('wilayah/create/' . $jenis) ?>">

                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nama <?= $jenis; ?></label>
                                <div class="col-sm-10">
                                    <input type="text" name="nama_wilayah" class="form-control" value="<?= set_value('nama_wilayah') ?>" autofocus>
                                    <?= form_error('nama_wilayah', '<small class="text-danger">', '</small>') ?>
                                </div>
                            </div>
                            <div class="form-group row">
                                <input type="submit" value="Simpan" class="btn btn-success">
                                <a href="<?= BASE_URL('wilayah') ?>" class="btn btn-warning ml-3">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>

            </div>

        </div>

    </div>

</div>

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

Buat controller Wilayah.php

<?php

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

class Wilayah 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('wilayah_model');
    }

    public function index()
    {
        $data['title']  = 'Wilayah';
        $data['page']   = 'pages/wilayah';
        $data['user']   = $this->user;
        $data['lokasi'] = $this->wilayah_model->read('tb_lokasi');
        $data['negara'] = $this->wilayah_model->read('tb_negara');

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

    public function create($jenis)
    {
        $this->form_validation->set_rules('nama_wilayah', 'Wilayah', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['title'] = 'Input';
            $data['jenis'] = $jenis;
            $data['page']  = 'pages/wilayah_create';
            $data['user']  = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            if ($this->wilayah_model->create($jenis)) {
                return redirect(base_url('wilayah'));
            } else {
                return 'terjadi kesalahan';
            }
        }
    }
}

/* End of file Wilayah.php */

Ubah view layouts _sidebar.php untuk menambahkan navigasi ke halaman wilayah (default: index)

<?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="#">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 -->

Update – Wilayah

Tambahkan method readById dan method update pada model wilayah

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

class Wilayah_model extends CI_Model
{
    public function read(String $tabel)
    {
        return $this->db->get($tabel)->result();
    }

    public function create($jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
            $data = [
                'nama_lokasi' => $this->input->post('nama_wilayah')
            ];
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
            $data = [
                'nama_negara' => $this->input->post('nama_wilayah')
            ];
        }

        return $this->db->insert($tabel, $data);
    }

    public function readById($id, $jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
        }

        $nama = 'nama_' . $jenis . ' AS nama';

        return $this->db->select('*,' . $nama)->where('id', $id)->get($tabel)->row_object();
    }

    public function update($id, $jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
            $data = [
                'nama_lokasi' => $this->input->post('nama_wilayah')
            ];
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
            $data = [
                'nama_negara' => $this->input->post('nama_wilayah')
            ];
        }

        return $this->db->update($tabel, $data, array('id' => $id));
    }
}

Buat view dalam pages dengan nama wilayah_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"><?= $jenis; ?></h6>
                </div>
                <div class="card-body">
                    <div class="card-body">
                        <form method="post" accept-charset="utf-8" action="<?= BASE_URL('wilayah/edit/' . $jenis . '/' . $wilayah->id) ?>">

                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">nama wilayah</label>
                                <div class="col-sm-10">
                                    <input type="text" name="nama_wilayah" class="form-control" placeholder="nama wilayah" value="<?= set_value('nama_wilayah', $wilayah->nama) ?>">
                                    <?= form_error('nama_wilayah', '<small class="text-danger">', '</small>') ?>
                                </div>
                            </div>
                            <div class="form-group row">
                                <input type="submit" value="Ubah" class="btn btn-success">
                                <a href="<?= BASE_URL('wilayah') ?>" class="btn btn-warning ml-3">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>

            </div>

        </div>

    </div>

</div>

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

Ubah view wilayah.php dan ganti tombol aksi 1 menjadi tombol ubah mengarah ke edit dan diikuti id wilayah

<?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('wilayah/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">Kelola Wilayah</h6>
                </div>

                <div class="row">

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/lokasi') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Lokasi</a>
                            <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 Lokasi</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($lokasi as $location) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $location->nama_lokasi ?></td>
                                            <td>
                                                <a href="<?= BASE_URL('wilayah/edit/lokasi/' . $location->id) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                                <a href="#" class="btn btn-sm btn-danger">Aksi 2</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/negara') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Negara</a>
                            <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 Negara</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($negara as $country) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $country->nama_negara ?></td>
                                            <td>
                                                <a href="<?= BASE_URL('wilayah/edit/negara/' . $country->id) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                                <a href="#" class="btn btn-sm btn-danger">Aksi 2</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>

            </div>

        </div>

    </div>

</div>

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

Ubah controller Wilayah.php untuk menambahkan method edit

<?php

use function PHPSTORM_META\type;

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

class Wilayah 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('wilayah_model');
    }

    public function index()
    {
        $data['title']  = 'Wilayah';
        $data['page']   = 'pages/wilayah';
        $data['user']   = $this->user;
        $data['lokasi'] = $this->wilayah_model->read('tb_lokasi');
        $data['negara'] = $this->wilayah_model->read('tb_negara');

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

    public function create($jenis)
    {
        $this->form_validation->set_rules('nama_wilayah', 'Wilayah', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['title'] = 'Input';
            $data['jenis'] = $jenis;
            $data['page']  = 'pages/wilayah_create';
            $data['user']  = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            if ($this->wilayah_model->create($jenis)) {
                return redirect(base_url('wilayah'));
            } else {
                return 'terjadi kesalahan';
            }
        }
    }

    public function edit($jenis, $id)
    {
        $this->form_validation->set_rules('nama_wilayah', 'Wilayah', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['title']   = 'Update';
            $data['jenis'] = $jenis;
            $data['page']    = 'pages/wilayah_update';
            $data['user']    = $this->user;
            $data['wilayah'] = $this->wilayah_model->readById($id, $jenis);

            $this->load->view('layouts/app', $data);
        } else {
            if ($this->wilayah_model->update($id, $jenis)) {
                return redirect(base_url('wilayah'));
            }
        }
    }
}

/* End of file Wilayah.php */

Delete – Wilayah

Ubah view wilayah.php dan ganti tombol aksi 2 menjadi tombol hapus mengarah ke delete dan diikuti id wilayah

<?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('wilayah/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">Kelola Wilayah</h6>
                </div>

                <div class="row">

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/lokasi') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Lokasi</a>
                            <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 Lokasi</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($lokasi as $location) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $location->nama_lokasi ?></td>
                                            <td>
                                                <a href="<?= BASE_URL('wilayah/edit/lokasi/' . $location->id) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                                <a href="<?= BASE_URL('wilayah/delete/lokasi/' . $location->id) ?>" class="btn btn-sm btn-danger">Hapus</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="card-body col-6">
                        <div class="card-body">
                            <a href="<?= BASE_URL('wilayah/create/negara') ?>" class="btn btn-primary shadow-sm mb-2"><i class="fas fa-plus fa-sm text-white-50"></i> Negara</a>
                            <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 Negara</th>
                                        <th rowspan="1" colspan="1">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($negara as $country) : ?>
                                        <tr class="odd">
                                            <td class="sorting_1"><?= $country->nama_negara ?></td>
                                            <td>
                                                <a href="<?= BASE_URL('wilayah/edit/negara/' . $country->id) ?>" class="btn btn-sm btn-warning">Ubah</a>
                                                <a href="<?= BASE_URL('wilayah/delete/negara/' . $country->id) ?>" class="btn btn-sm btn-danger">Hapus</a>
                                            </td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>

            </div>

        </div>

    </div>

</div>

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

Tambah method delete pada model wilayah

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

class Wilayah_model extends CI_Model
{
    public function read(String $tabel)
    {
        return $this->db->get($tabel)->result();
    }

    public function create($jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
            $data = [
                'nama_lokasi' => $this->input->post('nama_wilayah')
            ];
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
            $data = [
                'nama_negara' => $this->input->post('nama_wilayah')
            ];
        }

        return $this->db->insert($tabel, $data);
    }

    public function readById($id, $jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
        }

        $nama = 'nama_' . $jenis . ' AS nama';

        return $this->db->select('*,' . $nama)->where('id', $id)->get($tabel)->row_object();
    }

    public function update($id, $jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
            $data = [
                'nama_lokasi' => $this->input->post('nama_wilayah')
            ];
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
            $data = [
                'nama_negara' => $this->input->post('nama_wilayah')
            ];
        }

        return $this->db->update($tabel, $data, array('id' => $id));
    }

    public function delete($id, $jenis)
    {
        if ($jenis == 'lokasi') {
            $tabel = 'tb_lokasi';
        } elseif ($jenis == 'negara') {
            $tabel = 'tb_negara';
        }
        return $this->db->delete($tabel, array('id' => $id));
    }
}

Tambah method delete pada controller wilayah

<?php

use function PHPSTORM_META\type;

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

class Wilayah 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('wilayah_model');
    }

    public function index()
    {
        $data['title']  = 'Wilayah';
        $data['page']   = 'pages/wilayah';
        $data['user']   = $this->user;
        $data['lokasi'] = $this->wilayah_model->read('tb_lokasi');
        $data['negara'] = $this->wilayah_model->read('tb_negara');

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

    public function create($jenis)
    {
        $this->form_validation->set_rules('nama_wilayah', 'Wilayah', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['title'] = 'Input';
            $data['jenis'] = $jenis;
            $data['page']  = 'pages/wilayah_create';
            $data['user']  = $this->user;

            $this->load->view('layouts/app', $data);
        } else {
            if ($this->wilayah_model->create($jenis)) {
                return redirect(base_url('wilayah'));
            } else {
                return 'terjadi kesalahan';
            }
        }
    }

    public function edit($jenis, $id)
    {
        $this->form_validation->set_rules('nama_wilayah', 'Wilayah', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['title']   = 'Update';
            $data['jenis'] = $jenis;
            $data['page']    = 'pages/wilayah_update';
            $data['user']    = $this->user;
            $data['wilayah'] = $this->wilayah_model->readById($id, $jenis);

            $this->load->view('layouts/app', $data);
        } else {
            if ($this->wilayah_model->update($id, $jenis)) {
                return redirect(base_url('wilayah'));
            }
        }
    }

    public function delete($jenis, $id)
    {
        if ($this->wilayah_model->delete($id, $jenis)) {
            return redirect(base_url('wilayah'));
        }
    }
}

/* End of file Wilayah.php */

Sekarang silakan uji halaman wilayah mulai dari menampilkan data (index / read), tambah data (create), mengubah data (update), dan menghapus data (delete)

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.

Comments

Tinggalkan Balasan