Tugas 9 - CRUD CodeIgniter
TUGAS-9 PBKK C
CRUD CodeIgniter
Oleh:
Evelyn Tjitrodjojo 05111840000099
Pada tugas kali ini, diminta untuk membuat sebuah website yang berisi create, read, update, and delete (CRUD) mahasiswa dengan menggunakan Code Igniter. Sebelumnya, lakukan beberapa hal berikut:
Untuk step by step tutorialnya akan dibahas dibawah ini:
1. Membuat database pada mysql
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mahasiswa',
3. Berikutnya, silahkan buka 📄
config/autoload.php
.Kemudian cari $autoload['libraries']
dan tambahkan database
dan session
di sana.
$autoload['libraries'] = array('database', 'session');
4. Buat file baru di dalam direktori 📁
application/model/
dengan nama Mahasiswa_model.php
. Lalu tuliskan kode berikut.<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Mahasiswa_model extends CI_Model
{
private $_table = "mahasiswa";
public $nrp;
public $nama;
public $alamat;
public $telepon;
public $jurusan;
public function rules()
{
return [
['field' => 'nrp',
'label' => 'Nrp',
'rules' => 'required'],
['field' => 'nama',
'label' => 'Nama',
'rules' => 'required'],
['field' => 'alamat',
'label' => 'Alamat',
'rules' => 'required'],
['field' => 'telepon',
'label' => 'Telepon',
'rules' => 'required'],
['field' => 'jurusan',
'label' => 'Jurusan',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($nrp)
{
return $this->db->get_where($this->_table, ["nrp" => $nrp])->row();
}
public function save()
{
$post = $this->input->post();
$this->nrp = $post["nrp"];
$this->nama = $post["nama"];
$this->alamat = $post["alamat"];
$this->telepon = $post["telepon"];
$this->jurusan = $post["jurusan"];
return $this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->nrp = $post["nrp"];
$this->nama = $post["nama"];
$this->alamat = $post["alamat"];
$this->telepon = $post["telepon"];
$this->jurusan = $post["jurusan"];
return $this->db->update($this->_table, $this, array('nrp' => $post['nrp']));
}
public function delete($nrp)
{
return $this->db->delete($this->_table, array("nrp" => $nrp));
}
}
5. Buat file baru di dalam folder 📁
application/controllers/admin/
dengan nama Mahasiswa.php
. Isi file Mahasiswa.php
dengan kode berikut.<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mahasiswa extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("mahasiswa_model");
$this->load->library('form_validation');
}
public function index()
{
$data["mahasiswa"] = $this->mahasiswa_model->getAll();
$this->load->view("mahasiswa/list", $data);
}
public function add()
{
$mahasiswa = $this->mahasiswa_model;
$validation = $this->form_validation;
$validation->set_rules($mahasiswa->rules());
if ($validation->run()) {
$mahasiswa->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("mahasiswa/new_form");
}
public function edit($id = null)
{
if (!isset($id)) redirect('admin/mahasiswa');
$mahasiswa = $this->mahasiswa_model;
$validation = $this->form_validation;
$validation->set_rules($mahasiswa->rules());
if ($validation->run()) {
$mahasiswa->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$data["mahasiswa"] = $mahasiswa->getById($id);
if (!$data["mahasiswa"]) show_404();
$this->load->view("mahasiswa/edit_form", $data);
}
public function delete($id=null)
{
if (!isset($id)) show_404();
if ($this->mahasiswa_model->delete($id)) {
redirect(site_url('mahasiswa'));
}
}
}
views/
dengan nama mahasiswa
.7. Buatlah file baru dengan nama
list.php
di dalam folder views/
mahasiswa. Setelah itu, isi dengan kode berikut:<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("_partials/breadcrumb.php") ?>
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/mahasiswa/add') ?>">
<i class="fas fa-plus"></i> Add New</a>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>NRP</th>
<th>Nama</th>
<th>Alamat</th>
<th>Telepon</th>
<th>Jurusan</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($mahasiswa as $mahasiswa): ?>
<tr>
<td width="150">
<?php echo $mahasiswa->nrp ?>
</td>
<td>
<?php echo $mahasiswa->nama ?>
</td>
<td>
<?php echo $mahasiswa->alamat ?>
</td>
<td>
<?php echo $mahasiswa->telepon ?>
</td>
<td>
<?php echo $mahasiswa->jurusan ?>
</td>
<td width="250">
<a href="<?php echo site_url('admin/mahasiswa/edit/'.$mahasiswa->nrp) ?>"
class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
<a onclick="deleteConfirm('<?php echo site_url('admin/mahasiswa/delete/'.$mahasiswa->nrp) ?>')"
href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- /.content-wrapper -->
<?php $this->load->view("_partials/scrolltop.php") ?>
<?php $this->load->view("_partials/js.php") ?>
</div>
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Are you sure?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Data yang dihapus tidak akan bisa dikembalikan.</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a id="btn-delete" class="btn btn-danger" href="#">Delete</a>
</div>
</div>
</div>
</div>
<script>
function deleteConfirm(url){
$('#btn-delete').attr('href', url);
$('#deleteModal').modal();
}
</script>
</body>
</html>
8. Silahkan buat file baru di dalam folder
view/mahasiswa/
dengan nama new_form.php
. Setelah itu, isi dengan kode berikut:<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/mahasiswa/') ?>">
<i class="fas fa-arrow-left"></i> Back</a>
</div>
<div class="card-body">
<form action="<?php echo site_url('admin/mahasiswa/add') ?>" method="post">
<div class="form-group">
<label for="nrp">NRP*</label>
<input class="form-control
<?php echo form_error('nrp') ? 'is-invalid':'' ?>"
type="text" name="nrp" placeholder="Nrp" />
<div class="invalid-feedback">
<?php echo form_error('nrp') ?>
</div>
</div>
<div class="form-group">
<label for="name">Nama*</label>
<input class="form-control
<?php echo form_error('nama') ? 'is-invalid':'' ?>"
type="text" name="nama" placeholder="Nama" />
<div class="invalid-feedback">
<?php echo form_error('nama') ?>
</div>
</div>
<div class="form-group">
<label for="alamat">Alamat*</label>
<input class="form-control
<?php echo form_error('alamat') ? 'is-invalid':'' ?>"
type="text" name="alamat" placeholder="Alamat" />
<div class="invalid-feedback">
<?php echo form_error('alamat') ?>
</div>
</div>
<div class="form-group">
<label for="telepon">Telepon*</label>
<input class="form-control
<?php echo form_error('telepon') ? 'is-invalid':'' ?>"
type="text" name="telepon" placeholder="Telepon" />
<div class="invalid-feedback">
<?php echo form_error('telepon') ?>
</div>
</div>
<div class="form-group">
<label for="jurusan">Jurusan*</label>
<input class="form-control
<?php echo form_error('jurusan') ? 'is-invalid':'' ?>"
type="text" name="jurusan" placeholder="Jurusan" />
<div class="invalid-feedback">
<?php echo form_error('jurusan') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
</div>
<?php $this->load->view("_partials/scrolltop.php") ?>
<?php $this->load->view("_partials/js.php") ?>
</div>
</body>
</html>
9. Silahkan buat file baru di dalam folder
views/mahasiswa/
dengan nama edit_form.php
. Setelah itu, isi dengan kode berikut:<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<!-- Card -->
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/mahasiswa/') ?>">
<i class="fas fa-arrow-left"></i>
Back</a>
</div>
<div class="card-body">
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $mahasiswa->nrp?>" />
<div class="form-group">
<label for="nrp">NRP*</label>
<input class="form-control
<?php echo form_error('nrp') ? 'is-invalid':'' ?>"
type="text" name="nrp" placeholder="Nrp"
value="<?php echo $mahasiswa->nrp ?>" />
<div class="invalid-feedback">
<?php echo form_error('nrp') ?>
</div>
</div>
<div class="form-group">
<label for="name">Nama*</label>
<input class="form-control
<?php echo form_error('nama') ? 'is-invalid':'' ?>"
type="text" name="nama" placeholder="Nama"
value="<?php echo $mahasiswa->nama ?>" />
<div class="invalid-feedback">
<?php echo form_error('nama') ?>
</div>
</div>
<div class="form-group">
<label for="alamat">Alamat*</label>
<input class="form-control
<?php echo form_error('alamat') ? 'is-invalid':'' ?>"
type="text" name="alamat" placeholder="Alamat" v
alue="<?php echo $mahasiswa->alamat ?>" />
<div class="invalid-feedback">
<?php echo form_error('alamat') ?>
</div>
</div>
<div class="form-group">
<label for="telepon">Telepon*</label>
<input class="form-control
<?php echo form_error('telepon') ? 'is-invalid':'' ?>"
type="text" name="telepon" placeholder="Telepon"
value="<?php echo $mahasiswa->telepon ?>" />
<div class="invalid-feedback">
<?php echo form_error('telepon') ?>
</div>
</div>
<div class="form-group">
<label for="jurusan">Jurusan*</label>
<input class="form-control
<?php echo form_error('jurusan') ? 'is-invalid':'' ?>"
type="text" name="jurusan" placeholder="Jurusan"
value="<?php echo $mahasiswa->jurusan ?>" />
<div class="invalid-feedback">
<?php echo form_error('jurusan') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
</div>
<?php $this->load->view("_partials/scrolltop.php") ?>
<?php $this->load->view("_partials/js.php") ?>
</div>
</body>
</html>
Code lengkapnya dapat dilihat pada link berikut
CODE : belajarCRUD_CodeIgniter
Untuk hasilnya akan seperti ini.
Comments
Post a Comment