Bismillah.
Insyaallah ini materi terakhir mata kuliah web 2.
Untuk eksplorasi sendiri, banyak di tutorial di Internet.
Tidak diberikan di sini, mengingat masa Covid-19 banyak teman-teman kita yang tidak punya laptop.
Bagi yang masih pusing, minimal teman-teman rajin berlatih sampai membuat tombol tambah dan fungsi tampilnya untuk persiapan UAS.
**
Agar tidak pusing, kita sekarang membuat file baru yaa...
1. Buka application/controller
2. Buatlah file Mhs.php
3. Salin script ini
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mhs extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('M_Mhs');
}
function index()
{
$data['hasil']=$this->M_Mhs->TampilMahasiswa();
$this->load->view('tasdik/v_mhs',$data);
}
function tambah()
{
$this->load->view('tasdik/v_tambah_mhs');
}
function simpan_mahasiswa()
{
$data = array(
'nim'=>$this->input->post('nim'),
'nama'=>$this->input->post('nama')
);
$this->db->insert('tasdik',$data);
redirect('mhs/index');
}
function update($nik)
{
$data['ambil']=$this->M_Mhs->GetNik($nik);
$this->load->view('tasdik/v_update_mhs',$data);
}
function simpan_update()
{
$data = array(
'nama'=>$this->input->post('nama')
);
$nik = $this->input->post('nim');
$this->db->where('nim', $nik);
$this->db->update('tasdik',$data);
redirect('mhs');
}
function hapus($nik)
{
$this->M_Mhs->HapusMahasiswa($nik);
redirect('mhs');
}
}
?>
4. Buka direktori application/views/folder masing-masing
5. Buatlah file v_mhs.php
6. Salinlah script ini:
<!DOCTYPE html>
<html>
<head>
<!-- Load file CSS Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- Load file library jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Load file library Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Load file library JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class='container'>
<table class="table table-bordered">
<thead>
<tr>
<th> No</th> <th>NIK</th> <th>Nama</th> <th colspan='2'>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($hasil as $item)
{
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $item->nim;?></td>
<td><?php echo $item->nama;?></td>
<td> <a href="<?php echo base_url()?>mhs/update/<?php echo $item->nim; ?>" class="btn btn-warning" role="button">Update</a></td>
<td> <a href="<?php echo base_url()?>mhs/hapus/<?php echo $item->nim; ?>" class="btn btn-danger" role="button">Delete</a></td>
</tr>
<?php
$no++;
}
?>
</tbody>
</table>
<a href="<?php echo base_url()?>mhs/tambah" class="btn btn-primary" role="button">Tambah Data</a>
</div>
</body>
</html>
7. Buka direktori application/views/folder masing-masing
8. Buatlah file v_tambah_mhs.php
9. Salinlah script ini:
<!DOCTYPE html>
<html>
<head>
<!-- Load file CSS Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- Load file library jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Load file library Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Load file library JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Tambah mahasiswa</h2>
<form action="<?php echo base_url()?>mhs/simpan_mahasiswa" method="post">
<div class="form-group">
<label>NIM:</label>
<input type="text" name="nim" class="form-control" placeholder="NIM" />
</div>
<div class="form-group">
<label>Nama:</label>
<input type="text" name="nama" class="form-control" placeholder="Nama" />
</div>
<button type="submit" class="btn btn-primary">Tambah</button>
</form>
</div>
</body>
</html>
10. Buka direktori application/views/folder masing-masing
11. Buatlah file v_update_mhs.php
12. Salinlah script ini:
<!DOCTYPE html>
<html>
<head>
<!-- Load file CSS Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- Load file library jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Load file library Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Load file library JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Update mahasiswa #<?php echo $ambil->nim; ?></h2>
<form action="<?php echo base_url()?>mhs/simpan_update" method="post">
<input type="hidden" name="nim" value="<?php echo $ambil->nim; ?>" />
<div class="form-group">
<label>Nama:</label>
<input type="text" name="nama" class="form-control" value="<?php echo $ambil->nama; ?>" placeholder="Nama" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</body>
</html>
13. Buka application/models
14. Buatlah file M_Mhs.php
15. Salinlah script ini:
<?php
Class M_Mhs extends CI_Model
{
function TampilMahasiswa()
{
$this->db->order_by('nim', 'ASC');
return $this->db->from('tasdik')
->get()
->result();
}
function GetNik($nik = '')
{
return $this->db->get_where('tasdik', array('nim' => $nik))->row();
}
function HapusMahasiswa($nik)
{
$this->db->delete('tasdik',array('nim' => $nik));
}
}
?>
16. Jalankan dengan link seperti ini
lab1.katabah.com/pagi/mhs
*mhs ganti dengan nama file masing-masing
*Kelas sore, ganti pagi menjadi sore
17. Selesai.
Tutorial Lainnya:
CI Pertemuan 4 Tombol Tambah Hingga Berfungsi (Di Hosting)
CI Pertemuan 3 Membuat Form Tambah di Code Igniter (Di Hosting)
"Boleh Konsultasi Masuk Jurusan Sistem Informasi via IG atau Tiktok."
|
Tips Skripsi Program Studi Sistem Informasi |
|
No comments:
Post a Comment