Notification texts go here Contact Us

Sistem Informasi Mahasiswa: CRUD dengan CodeIgniter 3

Contoh skrip sederhana untuk melakukan operasi CRUD (Create, Read, Update, Delete) dalam CodeIgniter 3 dengan database "db_mhs" dan tabel "tb_mhs". Pe
Contoh skrip sederhana untuk melakukan operasi CRUD (Create, Read, Update, Delete) dalam CodeIgniter 3 dengan database "db_mhs" dan tabel "tb_mhs". Pertama, pastikan telah mengkonfigurasi CodeIgniter dengan benar, termasuk pengaturan database.
Berikut adalah contoh kode untuk melakukan operasi CRUD:

A. CREATE (Insert Data) - Menambahkan Data:

Untuk menambahkan data ke tabel "tb_mhs", Anda bisa menggunakan controller dan model berikut:

Controller/MhsController.php:

[<?php
class MhsController extends CI_Controller {
    public function create() {
        $data = array(
            'NIM' => '123456',
            'Nama' => 'John Doe',
            'Alamat' => 'Jl. Contoh No. 123',
            'Agama' => 'Islam',
            'Hobi' => 'Membaca'
        );

        $this->load->model('MhsModel');
        $this->MhsModel->insert_data($data);

        echo 'Data berhasil ditambahkan!';
    }
}
?>]

Model/MhsModel.php:

[<?php
class MhsModel extends CI_Model {
    public function insert_data($data) {
        $this->db->insert('tb_mhs', $data);
    }
}
?>]

Web Browser:

http://localhost/[Project Name]/index.php/MhsController/create


B. READ (Tampilkan Data) - Menampilkan Data:

Untuk menampilkan data dari tabel "tb_mhs", Anda bisa menggunakan controller dan view berikut:

Controller/MhsController.php:

[<?php
class MhsController extends CI_Controller {
    public function read() {
        $this->load->model('MhsModel');
        $data['mahasiswa'] = $this->MhsModel->get_all_data();
        $this->load->view('mahasiswa_view', $data);
    }
}
?>]

Model/MhsModel.php:

[<?php
class MhsModel extends CI_Model {
    public function get_all_data() {
        $query = $this->db->get('tb_mhs');
        return $query->result();
    }
}
?>]

View/mahasiswa_view.php:

[<?php
foreach ($mahasiswa as $mhs) {
    echo 'NIM: ' . $mhs->NIM . '<br>';
    echo 'Nama: ' . $mhs->Nama . '<br>';
    echo 'Alamat: ' . $mhs->Alamat . '<br>';
    echo 'Agama: ' . $mhs->Agama . '<br>';
    echo 'Hobi: ' . $mhs->Hobi . '<br><br>';
}
?>]

Web Browser:

http://localhost/[Project Name]/index.php/MhsController/read


C. UPDATE (Perbarui Data) - Memperbarui Data:

Untuk memperbarui data di tabel "tb_mhs", Anda bisa menggunakan controller dan model berikut:

Controller/MhsController.php:

[<?php
class MhsController extends CI_Controller {
    public function update() {
        $data = array(
            'Nama' => 'Jane Doe',
            'Alamat' => 'Jl. Updated No. 456',
            'Agama' => 'Islam',
            'Hobi' => 'Bermain Musik'
        );

        $this->load->model('MhsModel');
        $this->MhsModel->update_data('123456', $data);

        echo 'Data berhasil diperbarui!';
    }
}
?>]

Model/MhsModel.php:

[<?php
class MhsModel extends CI_Model {
    public function update_data($NIM, $data) {
        $this->db->where('NIM', $NIM);
        $this->db->update('tb_mhs', $data);
    }
}
?>]

Web Browser:

http://localhost/[Project Name]/index.php/MhsController/update


D. DELETE (Hapus Data) - Menghapus Data:

Untuk menghapus data dari tabel "tb_mhs", Anda bisa menggunakan controller dan model berikut:

Controller/MhsController.php:

[<?php
class MhsController extends CI_Controller {
    public function delete() {
        $NIM = '123456';

        $this->load->model('MhsModel');
        $this->MhsModel->delete_data($NIM);

        echo 'Data berhasil dihapus!';
    }
}
?>]

Model/MhsModel.php:

[<?php
class MhsModel extends CI_Model {
    public function delete_data($NIM) {
        $this->db->where('NIM', $NIM);
        $this->db->delete('tb_mhs');
    }
}
?>]


Web Browser:

http://localhost/[Project Name]/index.php/MhsController/delete

 

Link Browser

[http://localhost/[Main Folder]/[Functions Folder]/index.php/MhsController/create]


CRUD, CI, CI3, CodeIgniter, Code Igniter, Mahasiswa

 

Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.