Tugas 7
CRUD Mahasiswa CodeIgniter
Database
Menggunakan table "mahasiswa" dengan beberapa atribut yang akan diisikan
Model
Membuat file model "MahasiswaModel" untuk menampung model data dari aplikasi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use CodeIgniter\Model; | |
class MahasiswaModel extends Model | |
{ | |
protected $table = 'mahasiswa'; | |
protected $useTimestamps = true; | |
protected $allowedFields = ['nrp', 'nama', 'alamat', 'jurusan']; | |
} |
Controller
Memodifikasi file controller "Home" untuk back end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controllers; | |
use App\Models\MahasiswaModel; | |
class Home extends BaseController | |
{ | |
protected $mahasiswaModel; | |
public function __construct() | |
{ | |
$this->mahasiswaModel = new MahasiswaModel(); | |
} | |
public function index() | |
{ | |
$mahasiswa = $this->mahasiswaModel->findAll(); | |
$data = [ | |
'mahasiswa' => $mahasiswa | |
]; | |
return view('index', $data); | |
} | |
public function add() | |
{ | |
return view('add'); | |
} | |
public function save() | |
{ | |
$this->mahasiswaModel->save([ | |
'nrp' => $this->request->getVar('nrp'), | |
'nama' => $this->request->getVar('nama'), | |
'alamat' => $this->request->getVar('alamat'), | |
'jurusan' => $this->request->getVar('jurusan') | |
]); | |
return redirect()->to('/'); | |
} | |
public function delete($id) | |
{ | |
$this->mahasiswaModel->delete($id); | |
return redirect()->to('/'); | |
} | |
public function edit($id) | |
{ | |
$mahasiswa = $this->mahasiswaModel->where(['id' => $id])->first(); | |
$data = [ | |
'mahasiswa' => $mahasiswa | |
]; | |
return view('edit', $data); | |
} | |
public function update($id) | |
{ | |
$this->mahasiswaModel->save([ | |
'id' => $id, | |
'nrp' => $this->request->getVar('nrp'), | |
'nama' => $this->request->getVar('nama'), | |
'alamat' => $this->request->getVar('alamat'), | |
'jurusan' => $this->request->getVar('jurusan') | |
]); | |
return redirect()->to('/'); | |
} | |
} |
View
Membuat template untuk view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="<?= base_url('assets/style.css') ?>"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> | |
<title>CRUD-Mahasiswa</title> | |
</head> | |
<body> | |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> | |
<div class="container"> | |
<a class="navbar-brand" href="<?= site_url('/') ?>">Home</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse" id="navbarNav"> | |
<ul class="navbar-nav"> | |
<li class="nav-item"> | |
<a class="nav-link" href="<?= site_url('/add') ?>">Add</a> | |
</li> | |
</ul> | |
</div> | |
</nav> | |
</div> | |
<main> | |
<div class="container"> | |
<?= $this->renderSection('content') ?> | |
</div> | |
</main> | |
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> | |
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script> | |
</body> | |
</html> |
Membuat beberapa view untuk tampilan website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= $this->extend('layout/template') ?> | |
<?= $this->section('content') ?> | |
<a href="<?= site_url('/add') ?>" class="btn btn-primary">Tambah Data</a> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th scope="col">NRP</th> | |
<th scope="col">Nama</th> | |
<th scope="col">Alamat</th> | |
<th scope="col">Jurusan</th> | |
<th scope="col">Aksi</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($mahasiswa as $data): ?> | |
<tr> | |
<td><?= $data['nrp'] ?></td> | |
<td><?= $data['nama'] ?></td> | |
<td><?= $data['alamat'] ?></td> | |
<td><?= $data['jurusan'] ?></td> | |
<td> | |
<a href="<?= site_url('/edit/' . $data['id']) ?>" class="btn btn-warning">Edit</a> | |
<form action="<?= site_url('/delete/' . $data['id']) ?>" method="post" class="d-inline"> | |
<?= csrf_field(); ?> | |
<input type="hidden" name="_method" value="DELETE"> | |
<button type="submit" class="btn btn-danger adminInventory_card-btnDelete" onclick="return confirm('Apakah anda yakin?');">Delete</button> | |
</form> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<?= $this->endsection() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= $this->extend('layout/template') ?> | |
<?= $this->section('content') ?> | |
<form action="<?= site_url('/save') ?>" method="POST"> | |
<?= csrf_field(); ?> | |
<div class="form-group"> | |
<label for="nrp">NRP</label> | |
<input type="text" class="form-control" id="nrp" name="nrp" placeholder="123456789"> | |
</div> | |
<div class="form-group"> | |
<label for="nama">Nama</label> | |
<input type="text" class="form-control" id="nama" name="nama" placeholder="Anggi"> | |
</div> | |
<div class="form-group"> | |
<label for="alamat">Alamat</label> | |
<input type="text" class="form-control" id="alamat" name="alamat" placeholder="Jalan Hoki no.1"> | |
</div> | |
<div class="form-group"> | |
<label for="jurusan">Jurusan</label> | |
<input type="text" class="form-control" id="jurusan" name="jurusan" placeholder="Teknik Informatika"> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<?= $this->endsection() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= $this->extend('layout/template') ?> | |
<?= $this->section('content') ?> | |
<form action="<?= site_url('/update/' . $mahasiswa['id']) ?>" method="POST"> | |
<?= csrf_field(); ?> | |
<div class="form-group"> | |
<label for="nrp">NRP</label> | |
<input type="text" class="form-control" id="nrp" name="nrp" placeholder="123456789" value="<?= $mahasiswa['nrp'] ?>"> | |
</div> | |
<div class="form-group"> | |
<label for="nama">Nama</label> | |
<input type="text" class="form-control" id="nama" name="nama" placeholder="Anggi" value="<?= $mahasiswa['nama'] ?>"> | |
</div> | |
<div class="form-group"> | |
<label for="alamat">Alamat</label> | |
<input type="text" class="form-control" id="alamat" name="alamat" placeholder="Jalan Hoki no.1" value="<?= $mahasiswa['alamat'] ?>"> | |
</div> | |
<div class="form-group"> | |
<label for="jurusan">Jurusan</label> | |
<input type="text" class="form-control" id="jurusan" name="jurusan" placeholder="Teknik Informatika" value="<?= $mahasiswa['jurusan'] ?>"> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<?= $this->endsection() ?> |
Hasil
Hasil dari view website
Comments
Post a Comment