Tugas 7 PBKK - CRUD Menggunakan CodeIgniter
1. Lakukan Instalasi Xampp dan CodeIgniter 3 Terlebih Dahulu.
2. Buatlah database menggunakan fitur MySQL yang ada di Xampp. Lalu, buat tabel berisi 7 kolom dan isi tabel tersebut seperti pada gambar dibawah ini.
3. Ubah setting database pada file database.php yang terletak di folder application/config.
4. Buatlah model dari aplikasi. Buat sebuah file bernama Siswa_model.php dan letakkan pada folder application/models.
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 defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Siswa_model extends CI_Model | |
{ | |
private $table = "tb_mahasiswa"; | |
public function getAll() | |
{ | |
return $this->db->get($this->table)->result(); | |
} | |
public function save($data) | |
{ | |
return $this->db->insert($this->table, $data); | |
} | |
public function getById($id) | |
{ | |
return $this->db->get_where($this->table, ["id_siswa" => $id])->row(); | |
} | |
public function update($data,$id) | |
{ | |
return $this->db->update($this->table, $data, array('id_siswa' => $id)); | |
} | |
public function delete($id) | |
{ | |
return $this->db->delete($this->table, array("id_siswa" => $id)); | |
} | |
} |
5. Buatlah file Siswa.php yang akan berperan sebagai controller dari aplikasi. Buat file ini pada folder application/controllers.
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 | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Siswa extends CI_Controller { | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->model("siswa_model"); | |
$this->load->library('form_validation'); | |
} | |
public function index() | |
{ | |
$data['siswa'] = $this->siswa_model->getAll(); | |
$this->load->view('template/header'); | |
$this->load->view('siswa/index',$data); | |
$this->load->view('template/footer'); | |
} | |
public function create() | |
{ | |
$this->load->view('template/header'); | |
$this->load->view('siswa/create'); | |
$this->load->view('template/footer'); | |
} | |
public function save() | |
{ | |
$this->form_validation->set_rules('nama','Nama','required'); | |
$this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required'); | |
$this->form_validation->set_rules('tempat_lahir','Tempat Lahir','required'); | |
$this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required'); | |
$this->form_validation->set_rules('no_telp','Nomor Telepon','required'); | |
$this->form_validation->set_rules('alamat','Alamat','required'); | |
if ($this->form_validation->run()==true) | |
{ | |
$data['nama'] = $this->input->post('nama'); | |
$data['jenis_kelamin'] = $this->input->post('jenis_kelamin'); | |
$data['tempat_lahir'] = $this->input->post('tempat_lahir'); | |
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir'); | |
$data['no_telp'] = $this->input->post('no_telp'); | |
$data['alamat'] = $this->input->post('alamat'); | |
$this->siswa_model->save($data); | |
redirect('siswa'); | |
} | |
else | |
{ | |
$this->load->view('template/header'); | |
$this->load->view('siswa/create'); | |
$this->load->view('template/footer'); | |
} | |
} | |
function edit($id_siswa) | |
{ | |
$data['siswa'] = $this->siswa_model->getById($id_siswa); | |
$this->load->view('template/header'); | |
$this->load->view('siswa/edit',$data); | |
$this->load->view('template/footer'); | |
} | |
public function update() | |
{ | |
$this->form_validation->set_rules('nama','Nama','required'); | |
$this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required'); | |
$this->form_validation->set_rules('tempat_lahir','Tempat Lahir','required'); | |
$this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required'); | |
$this->form_validation->set_rules('no_telp','Nomor Telepon','required'); | |
$this->form_validation->set_rules('alamat','Alamat','required'); | |
if ($this->form_validation->run()==true) | |
{ | |
$id_siswa = $this->input->post('id_siswa'); | |
$data['nama'] = $this->input->post('nama'); | |
$data['jenis_kelamin'] = $this->input->post('jenis_kelamin'); | |
$data['tempat_lahir'] = $this->input->post('tempat_lahir'); | |
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir'); | |
$data['no_telp'] = $this->input->post('no_telp'); | |
$data['alamat'] = $this->input->post('alamat'); | |
$this->siswa_model->update($data,$id_siswa); | |
redirect('siswa'); | |
} | |
else | |
{ | |
$id_siswa = $this->input->post('id_siswa'); | |
$data['siswa'] = $this->siswa_model->getById($id_siswa); | |
$this->load->view('template/header'); | |
$this->load->view('siswa/edit',$data); | |
$this->load->view('template/footer'); | |
} | |
} | |
function delete($id_siswa) | |
{ | |
$this->siswa_model->delete($id_siswa); | |
redirect('siswa'); | |
} | |
} |
- index.php
- create.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
<main role="main" class="container"> | |
<div class="card"> | |
<div class="card-header">Data Siswa</div> | |
<div class="card-body"> | |
<a href="<?php echo base_url(); ?>siswa/create" class="btn btn-success">Create</a> | |
<br/> | |
<br/> | |
<table class="table table-bordered"> | |
<tr> | |
<th width="5%">No</th> | |
<th>Nama</th> | |
<th>Jenis Kelamin</th> | |
<th>Tanggal Lahir</th> | |
<th>Tempat Lahir</th> | |
<th>Nomor Telepon</th> | |
<th>Alamat</th> | |
<th>Action</th> | |
</tr> | |
<?php | |
$no = 1; | |
foreach($siswa as $row) | |
{ | |
?> | |
<tr> | |
<td widtd="5%"><?php echo $no++; ?></td> | |
<td><?php echo $row->nama; ?></td> | |
<td><?php echo $row->jenis_kelamin; ?></td> | |
<td><?php echo $row->tanggal_lahir; ?></td> | |
<td><?php echo $row->tempat_lahir; ?></td> | |
<td><?php echo $row->no_telp; ?></td> | |
<td><?php echo $row->alamat; ?></td> | |
<td> | |
<a href="<?php echo base_url(); ?>siswa/edit/<?php echo $row->id_siswa; ?>" class="btn btn-warning">Edit</a> | |
<a href="<?php echo base_url(); ?>siswa/delete/<?php echo $row->id_siswa; ?>" class="btn btn-danger">Hapus</a> | |
</td> | |
</tr> | |
<?php | |
} | |
?> | |
</table> | |
</div> | |
</div> | |
</main> |
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
<div class="container"> | |
<div class="card"> | |
<div class="card-header">Create Siswa</div> | |
<div class="card-body"> | |
<?php | |
if(validation_errors() != false) | |
{ | |
?> | |
<div class="alert alert-danger" role="alert"> | |
<?php echo validation_errors(); ?> | |
</div> | |
<?php | |
} | |
?> | |
<form method="post" action="<?php echo base_url(); ?>siswa/save"> | |
<div class="form-group"> | |
<label for="nama">Nama</label> | |
<input type="text" class="form-control" id="nama" name="nama"> | |
</div> | |
<div class="form-group"> | |
<label for="jenis_kelamin">Jenis Kelamin</label> | |
<select name="jenis_kelamin" id="jenis_kelamin" class="form-control"> | |
<option value="L">Laki-laki</option> | |
<option value="P">Perempuan</option> | |
</select> | |
</div> | |
<div class="form-group"> | |
<label for="tempat_lahir">Tempat Lahir</label> | |
<input type="text" class="form-control" id="tempat_lahir" name="tempat_lahir"> | |
</div> | |
<div class="form-group"> | |
<label for="tgl_lahir">Tanggal Lahir</label> | |
<input type="text" class="form-control datepicker" id="tanggal_lahir" name="tanggal_lahir"> | |
</div> | |
<div class="form-group"> | |
<label for="no_telp">No Telp</label> | |
<input type="number" class="form-control" id="no_telp" name="no_telp"> | |
</div> | |
<div class="form-group"> | |
<label for="alamat">Alamat</label> | |
<textarea class="form-control" name="alamat" id="alamat"></textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
</div> | |
</div> | |
</div> |
- edit.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
<div class="container"> | |
<div class="card"> | |
<div class="card-header">Edit Siswa</div> | |
<div class="card-body"> | |
<?php | |
if(validation_errors() != false) | |
{ | |
?> | |
<div class="alert alert-danger" role="alert"> | |
<?php echo validation_errors(); ?> | |
</div> | |
<?php | |
} | |
?> | |
<form method="post" action="<?php echo base_url(); ?>siswa/update"> | |
<input type="hidden" name="id_siswa" id="id_siswa" value="<?php echo $siswa->id_siswa; ?>"/> | |
<div class="form-group"> | |
<label for="nama">Nama</label> | |
<input type="text" value="<?php echo $siswa->nama; ?>" class="form-control" id="nama" name="nama"> | |
</div> | |
<div class="form-group"> | |
<label for="jenis_kelamin">Jenis Kelamin</label> | |
<select name="jenis_kelamin" id="jenis_kelamin" class="form-control"> | |
<option value="L" <?php echo ($siswa->jenis_kelamin ? 'L' : 'selected' ); ?> >Laki-laki</option> | |
<option value="P" <?php echo ($siswa->jenis_kelamin ? 'P' : 'selected' ); ?>>Perempuan</option> | |
</select> | |
</div> | |
<div class="form-group"> | |
<label for="tempat_lahir">Tempat Lahir</label> | |
<input type="text" class="form-control" id="tempat_lahir" name="tempat_lahir" value="<?php echo $siswa->tempat_lahir; ?>"> | |
</div> | |
<div class="form-group"> | |
<label for="tgl_lahir">Tanggal Lahir</label> | |
<input type="text" class="form-control datepicker" readonly id="tanggal_lahir" name="tanggal_lahir" value="<?php echo $siswa->tanggal_lahir; ?>"> | |
</div> | |
<div class="form-group"> | |
<label for="no_telp">No Telp</label> | |
<input type="number" class="form-control" value="<?php echo $siswa->no_telp; ?>" id="no_telp" name="no_telp"> | |
</div> | |
<div class="form-group"> | |
<label for="alamat">Alamat</label> | |
<textarea class="form-control" name="alamat" id="alamat"><?php echo $siswa->alamat; ?></textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Update</button> | |
</form> | |
</div> | |
</div> | |
</div> |
7. Ketika aplikasi dijalankan, hasilnya akan seperti pada gambar dibawah ini
- Menambahkan mahasiswa baru ke dalam list
- List mahasiswa setelah ditambahkan
- Melakukan edit data alamat mahasiswa
- Data alamat mahasiswa akan berubah
- Hasil di database
Link Github: https://github.com/patrick-cw/crud-app
Comments
Post a Comment