Tugas 7 - CRUD CodeIgniter
Membuat CRUD Mahasiswa dengan CodeIgniter
Dalam pembuatan project ini pastikan di laptop maupun PC anda sudah terinstall PHP dan Composer. Untuk cara menginstallnya dapat di lihat di link di bawah ini.
- Install PHP
- Install Composer
1. Membuat Project baru dengan Composer
Untuk membuat project baru codeigniter, ketikkan syntax berikut ini dalam terminal:
composer create-project codeigniter4/appstarter NamaProject --no-dev
2. Konfigurasi
Untuk menggunakan codeigniter kita harus melakukan beberapa configurasi.
1. Edit file env menjadi .env agar menjadi file system
3. Menghubungkan ke Database
Untuk menghubungkan ke database kita dapat melakukannya dengan mengedit file database.php yang berada di App/Config/Database.php.
Masukkan username dan password akses ke database anda dan masukkan nama database yang anda ingin connectkan.
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 ModelMahasiswa extends Model | |
{ | |
function __construct() | |
{ | |
$this->db = db_connect(); | |
} | |
function tampildata() | |
{ | |
return $this->db->table('mahasiswa')->get(); | |
} | |
function simpan($data) | |
{ | |
return $this->db->table('mahasiswa')->insert($data); | |
} | |
function hapusData($idMhs) | |
{ | |
return $this->db->table('mahasiswa')->delete(['id_mhs' => $idMhs]); | |
} | |
function ambilData($idMhs) | |
{ | |
return $this->db->table('mahasiswa')->getWhere(['id_mhs' => $idMhs]); | |
} | |
function editData($data, $idMhs) | |
{ | |
return $this->db->table('mahasiswa')->update($data, ['id_mhs' => $idMhs]); | |
} | |
} |
5. Membuat Controller
Mahasiswa.php (terletak pada folder App/Controllers/Mahasiswa.php)
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 CodeIgniter\Controller; | |
use App\Models\ModelMahasiswa; | |
use Config\View; | |
class Mahasiswa extends Controller | |
{ | |
public function index() | |
{ | |
$mhs = new ModelMahasiswa(); | |
$data = [ | |
'tampildata' => $mhs->tampildata()->getResult() | |
]; | |
echo View('ViewTampilMahasiswa', $data); | |
} | |
public function formTambah() | |
{ | |
helper('form'); | |
echo View('viewFormTambah'); | |
} | |
public function simpanData() | |
{ | |
$data = [ | |
'nama' => $this->request->getpost('nama'), | |
'alamat' => $this->request->getpost('alamat'), | |
'no_hp' => $this->request->getpost('telp'), | |
'jenis_kel' => $this->request->getpost('jenisKel'), | |
]; | |
$mhs = new ModelMahasiswa(); | |
$simpan = $mhs->simpan($data); | |
if ($simpan) { | |
return redirect()->to('/mahasiswa'); | |
} | |
} | |
function hapus() | |
{ | |
$uri = service('uri'); | |
$idMhs = $uri->getSegment('3'); | |
$mhs = new ModelMahasiswa(); | |
$mhs->hapusData($idMhs); | |
return redirect()->to('/mahasiswa'); | |
} | |
function formEdit() | |
{ | |
helper('form'); | |
$uri = service('uri'); | |
$idMhs = $uri->getSegment('3'); | |
$mhs = new ModelMahasiswa(); | |
$ambilData = $mhs->ambilData($idMhs); | |
if (count($ambilData->getResult()) > 0) { | |
$row = $ambilData->getRow(); | |
$data = [ | |
'idMhs' => $idMhs, | |
'nama' => $row->nama, | |
'alamat' => $row->alamat, | |
'telp' => $row->no_hp, | |
'jenisKel' => $row->jenis_kel, | |
]; | |
echo View('viewFormEdit', $data); | |
} | |
} | |
function updateData() | |
{ | |
// $uri = service('uri'); | |
// $idMhs = $uri->getSegment('3'); | |
$idMhs = $this->request->getpost('idMhs'); | |
echo $idMhs; | |
$data = [ | |
'nama' => $this->request->getpost('nama'), | |
'alamat' => $this->request->getpost('alamat'), | |
'no_hp' => $this->request->getpost('telp'), | |
'jenis_kel' => $this->request->getpost('jenisKel'), | |
]; | |
$mhs = new ModelMahasiswa(); | |
$simpan = $mhs->editData($data, $idMhs); | |
if ($simpan) { | |
return redirect()->to('/mahasiswa'); | |
} | |
} | |
} |
6. Membuat View
View terletak pada folder App/View
- ViewTampilMahasiswa.php
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"> | |
<title>Data Mahasiswa</title> | |
<style> | |
table { | |
width: 100%; | |
border-collapse: collapse; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Data Mahasiswa</h2> | |
<p> | |
<!-- <button type="button" onclick="window.location='<?php echo site_url('mahasiswa/formTambah') ?>'"> | |
Tambah Data | |
</button> --> | |
<button type="button" onclick="window.location='../mahasiswa/formTambah'"> | |
Tambah Data | |
</button> | |
</p> | |
<table border="1"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Nama Mahasiswa</th> | |
<th>Alamat</th> | |
<th>No Tlp</th> | |
<th>Jenis Kelamin</th> | |
<th>Aksi</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
foreach ($tampildata as $row) : | |
?> | |
<tr> | |
<td><?= $row->id_mhs ?></td> | |
<td><?= $row->nama ?></td> | |
<td><?= $row->alamat ?></td> | |
<td><?= $row->no_hp ?></td> | |
<td><?= $row->jenis_kel ?></td> | |
<td> | |
<button type="button" onclick="hapus('<?= $row->id_mhs ?>')">Hapus</button> | |
<button type="button" onclick="window.location='../mahasiswa/formEdit/'+('<?= $row->id_mhs ?>')">Edit</button> | |
</td> | |
</tr> | |
<?php | |
endforeach; | |
?> | |
</tbody> | |
</table> | |
<script> | |
function hapus(idMhs) { | |
pesan = confirm('Yakin hapus data mahasiswa?'); | |
if (pesan) { | |
window.location = '../mahasiswa/hapus/' + idMhs; | |
} else return false; | |
} | |
</script> | |
</body> | |
</html> |
- viewFormTambah.php
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"> | |
<title>Form Tambah Mahasiswa</title> | |
</head> | |
<body> | |
<h2>Form Tambah Mahasiswa</h2> | |
<p> | |
<!-- <button type="button" onclick="window.location='<?php echo site_url('mahasiswa') ?>'"> | |
Kembali | |
</button> --> | |
<button type="button" onclick="window.location='../mahasiswa'"> | |
Kembali | |
</button> | |
<p> | |
<?= form_open('mahasiswa/simpanData') ?> | |
<table> | |
<tr> | |
<td>Nama :</td> | |
<td> | |
<input type="text" name="nama" size="50"> | |
</td> | |
</tr> | |
<tr> | |
<td>Alamat :</td> | |
<td> | |
<textarea name="alamat" id="alamat" cols="30" rows="5"></textarea> | |
</td> | |
</tr> | |
<tr> | |
<td>Telp :</td> | |
<td> | |
<input type="text" name="telp" pattern="{0-9}+"> | |
</td> | |
</tr> | |
<tr> | |
<td>Jenis Kelamin :</td> | |
<td> | |
<input type="radio" name="jenisKel" value="L">Laki-Laki | |
<input type="radio" name="jenisKel" value="P">Perempuan | |
</td> | |
</tr> | |
<tr> | |
<td></td> | |
<td> | |
<input type="submit" value="Simpan Data"> | |
</td> | |
</tr> | |
</table> | |
<?= form_close() ?> | |
</p> | |
</p> | |
</body> | |
</html> |
- viewFormEdit.php
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"> | |
<title>Form Edit Mahasiswa</title> | |
</head> | |
<body> | |
<h2>Form Edit Mahasiswa</h2> | |
<p> | |
<!-- <button type="button" onclick="window.location='<?php echo site_url('mahasiswa') ?>'"> | |
Kembali | |
</button> --> | |
<button type="button" onclick="window.location='../../mahasiswa'"> | |
Kembali | |
</button> | |
<p> | |
<?= form_open('mahasiswa/updateData') ?> | |
<table> | |
<tr> | |
<td>ID :</td> | |
<td> | |
<input type="text" name="idMhs" size="50" value="<?= $idMhs; ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td>Nama :</td> | |
<td> | |
<input type="text" name="nama" size="50" value="<?= $nama; ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td>Alamat :</td> | |
<td> | |
<textarea name="alamat" id="alamat" cols="30" rows="5"><?= $alamat; ?></textarea> | |
</td> | |
</tr> | |
<tr> | |
<td>Telp :</td> | |
<td> | |
<input type="text" name="telp" pattern="{0-9}+" value="<?= $telp; ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td>Jenis Kelamin :</td> | |
<td> | |
<input type="radio" name="jenisKel" value="L" <?php if ($jenisKel == 'L') echo 'checked'; ?>>Laki-Laki | |
<input type="radio" name="jenisKel" value="P" <?php if ($jenisKel == 'P') echo 'checked'; ?>>Perempuan | |
</td> | |
</tr> | |
<tr> | |
<td></td> | |
<td> | |
<input type="submit" value="Update Data"> | |
</td> | |
</tr> | |
</table> | |
<?= form_close() ?> | |
</p> | |
</p> | |
</body> | |
</html> |
Hasil
Tampilan data mahasiswa
Jika menekan tombol tamba data akan ke halaman formTambah dan akan tampil seperti berikut
Kemudian masukkan data yang ingin di tambahkan
Klik Simpan Data dan akan menambahkan data seperti gambar di bawah ini
Kemudian jika kita ingin menghapus salah satu data akan muncul peringatan apakah ingin menghapus atau tidak, jika iya maka data akan terhapus
Ketika di peringatan menghapus data di tekan iya maka akan menapilkan data yang baru dengan data yang dihapus tidak ada lagi
Komentar
Posting Komentar