Tugas 7 PBKK - CRUD Menggunakan CodeIgniter

Pada tugas kali ini, saya diminta untuk membuat aplikasi website dengan menggunakan framework CodeIgniter dengan proses CREATE, READ, UPDATE, dan DELETE.

CodeIgniter menjadi sebuah framework PHP dengan model MVC (Model, View, Controller) untuk membangun website dinamis dengan menggunakan PHP yang dapat mempercepat pengembang untuk membuat sebuah aplikasi web.


Berikut adalah beberapa hal yang perlu lakukan sebelum membuat Aplikasi CodeIgniter.


2. Instal CodeIgniter 3: https://codeigniter.com/download


Setelah menginstall codeigniter, maka berikut adalah langkah-langkah untuk membuat proses CRUD dengan menggunakan CodeIgniter.

1. Buatlah database dan sebuah tabel yang bernama "students" dengan struktur tabel sebagai berikut


2. Atur database.php pada config



3. Buat model untuk students

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

class Student_model extends CI_Model
{
    private $_table = "students";

    public $id;
    public $name;
    public $gender;
    public $birthplace;
    public $birthdate;
    public $address;
    public $phone;

    public function rules()
    {
        return [
            ['field' => 'name',
            'label' => 'Name',
            'rules' => 'required'],

            ['field' => 'gender',
            'label' => 'Gender',
            'rules' => 'required'],
            
            ['field' => 'birthplace',
            'label' => 'Birthplace',
            'rules' => 'required'],

            ['field' => 'birthdate',
            'label' => 'Birthdate',
            'rules' => 'required'],

            ['field' => 'address',
            'label' => 'Address',
            'rules' => 'required'],

            ['field' => 'phone',
            'label' => 'Phone',
            'rules' => 'required']
        ];
    }

    public function getAll()
    {
        return $this->db->get($this->_table)->result();
    }
    
    public function getById($id)
    {
        return $this->db->get_where($this->_table, ["id" => $id])->row();
    }

    public function create()
    {
        $post = $this->input->post();
        $this->id = uniqid();
        $this->name = $post["name"];
        $this->gender = $post["gender"];
        $this->birthplace = $post["birthplace"];
        $this->birthdate = $post["birthdate"];
        $this->address = $post["address"];
        $this->phone = $post["phone"];
        return $this->db->insert($this->_table, $this);
    }

    public function update()
    {
        $post = $this->input->post();
        $this->id = $post["id"];
        $this->name = $post["name"];
        $this->gender = $post["gender"];
        $this->birthplace = $post["birthplace"];
        $this->birthdate = $post["birthdate"];
        $this->address = $post["address"];
        $this->phone = $post["phone"];
        return $this->db->update($this->_table, $this, array('id' => $post['id']));
    }

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

4. Buat controller dan tambahkan fungsi berikut pada controller

    function __construct(){
        parent::__construct();        
        $this->load->model('student_model');
        $this->load->library('form_validation');
    }

    public function index()
    {
        $data["students"] = $this->student_model->getAll();
        $this->load->view('detail', $data);
    }

    public function addStudent()
    {
        $student = $this->student_model;
        $validation = $this->form_validation;
        $validation->set_rules($student->rules());

        if ($validation->run()) {
            $student->create();
            $this->session->set_flashdata('success', 'Berhasil disimpan');
        }
        $this->load->view('add_student');
    }

     public function editStudent($id = null)
    {
        if (!isset($id)) redirect('welcome/');
       
        $student = $this->student_model;
        $validation = $this->form_validation;
        $validation->set_rules($student->rules());

        if ($validation->run()) {
            $student->update();
            $this->session->set_flashdata('success', 'Berhasil disimpan');
        }

        $data["student"] = $student->getById($id);
        if (!$data["student"]) show_404();
        
        $this->load->view("edit_student", $data);
    }

    public function deleteStudent($id = null)
    {
        if (!isset($id)) show_404();
        
        if ($this->student_model->delete($id)) {
            redirect(site_url('welcome/'));
        }
    }


5. Buat form untuk menambahkan data siswa


    Apabila data berhasil ditambahkan akan muncul alert sebagai berikut



6. Buat tampilan (view) untuk menampilkan list siswa


7. Tambah data dan data berhasil ditambahkan


8. Buat form untuk mengubah data siswa

    
    Apabila data berhasil diubah



9. Tambahkan button pada tampilan (view) list siswa untuk UPDATE dan DELETE



Comments

Popular posts from this blog

Tugas 2 PBKK - Aplikasi .NET Framework

Tugas 8 PBKK - Company Profile Menggunakan Laravel

Tugas 1 PBKK - Portofolio Aplikasi Framework