Membuat View dan Controller Buku
Buat view input buku
Simpan pada folder 'application/view' dengan nama file buku_form_v.php
<?php
echo form_open('buku/save/'.$is_update);
echo form_hidden('id','');
echo "<table><tr><td>";
echo "Judul </td><td>:</td><td> ".form_input('judul','',"size=50' maxlength='100'");
echo "<br></td></tr>";
echo "<tr><td>Pengarang</td> <td> :</td><td> ".form_input('pengarang',set_value('pengarang','',"size=50' maxlength='100'");
echo "</td></tr>";
echo "<tr><td>Terbit</td> <td> :</td><td> ".form_input('terbit','',"size=50' maxlength='100'");
echo "</td></tr>";
echo "<tr><td>Kategori</td> <td> :</td><td> ".form_dropdown('kategori',$opt_kategori,'');
echo "</td></tr><tr><td></td><td></td><td>";
echo form_submit('btn_simpan','Simpan');
echo "</td></tr></table>";
echo form_close();
?>
Buat Controller Buku
Simpan pada folder 'application/controller' dengan nama file Buku.php
<?php
class Buku extends CI_Controller{
var $data = array();
function __construct(){
parent :: __construct();
$this->load->helper('form');
$this->load->helper('url');
$this->data['opt_kategori'] = array('' => '- Pilih Salah Satu -',
'Novel' => 'Novel',
'Komik' => 'Komik',
'Kamus' => 'Kamus');
$this->load->model('Buku_m');
}
function index()
{
$this->add_new();
}
function add_new(){
$this->data['is_update']= 0;
$this->load->view('buku_form_v',$this->data);
}
function save($is_update=0)
{
$data['Judul'] = $this->input->post('judul', true);
$data['Pengarang'] = $this->input->post('pengarang', true);
$data['Terbit'] = $this->input->post('terbit', true);
$data['Kategori'] = $this->input->post('kategori', true);
if($is_update==0)
{
//jika tambah data baru
if($this->Buku_m->insert($data))
redirect('buku');
}
else
{
//jika update data
$id = $this->input->post('id');
if($this->Buku_m->update_by_id($data, $id))
redirect('buku');
}
}
}
?>
Lakukan uji coba
http://localhost/perpus/index.php/buku/index
dan lihat hasil nya
Membuat view daftar buku
Simpan di folder 'application/view' beri nama file buku_v.php
<?php
echo "<h2>Data Buku FTIK</h2>";
echo anchor('buku/add_new/','Tambah Buku');
echo "<br><br>";
echo "<table align='center' border='1'>
<tr>
<th>No</th>
<th>Judul</th>
<th>Pengarang</th>
<th>Tempat Terbit</th>
<th>Kategori</th>
<th>Aksi</th>
</tr>";
$no = 0;
foreach($query->result_array() as $row)
{
$no++;
$kategori = $row['Kategori'];
$link_edit = anchor('buku/edit/'.$row['ID_Buku'], 'Edit');
$link_delete = anchor('buku/delete/'.$row['ID_Buku'],'Hapus', "onclick='return confirm(\"Yakin akan menghapus data ini?\")'");
echo "<tr>
<td>".$no."</td>
<td>".$row['Judul']."</td>
<td>".$row['Pengarang']."</td>
<td>".$row['Terbit']."</td>
<td>".$opt_kategori[$kategori]."</td>
<td>".$link_edit.' '.$link_delete."</td>
</tr>";
}
echo "</table>";
?>
Ubah method index pada controller buku
function index()
{
$this->data['query'] = $this->Buku_m->get_records();
$this->load->view('buku_v',$this->data);
}
Lakukan uji coba
http://localhost/perpus/index.php/buku/index
Tambahkan fungsi edit pada controller buku
function edit($id)
{
$this->data['query'] = $this->Buku_m->get_records("ID_Buku = '$id'");
$this->data['is_update'] = 1;
$this->load->view('buku_form_v',$this->data);
}
Lakukan modifikasi pada file buku_form_v.php
<?php
if (!empty($query))
{
$row = $query->row_array();
}
else
{
$row['ID_Buku'] = '';
$row['Judul'] = '';
$row['Pengarang'] = '';
$row['Terbit'] = '';
$row['Kategori'] = '';
}
echo form_open('buku/save/'.$is_update);
echo form_hidden('id',$row['ID_Buku']);
echo "<table><tr><td>";
echo "Judul </td><td>:</td><td> ".form_input('judul',set_value('judul',$row['Judul']),"size=50' maxlength='100'");
echo "<br></td></tr>";
echo "<tr><td>Pengarang</td> <td> :</td><td> ".form_input('pengarang',set_value('pengarang',$row['Pengarang']),"size=50' maxlength='100'");
echo "</td></tr>";
echo "<tr><td>Terbit</td> <td> :</td><td> ".form_input('terbit',set_value('terbit',$row['Terbit']),"size=50' maxlength='100'");
echo "</td></tr>";
echo "<tr><td>Kategori</td> <td> :</td><td> ".form_dropdown('kategori',$opt_kategori,set_value('kategori',$row['Kategori']));
echo "</td></tr><tr><td></td><td></td><td>";
echo form_submit('btn_simpan','Simpan');
echo "</td></tr></table>";
echo form_close();
?>
Lakukan pengujian edit dan pilih salah satu data yang akan di edit
http://localhost/index.php/buku/index
Selanjutnya tambahkan fungsi delete pada controller buku
function delete($id){
if($this->Buku_m->delete_by_id($id))
{
redirect('buku');
}
}
Lakukan pengujian fungsi delete dan pilih salah satu data yang ingin di hapus
Selamat mencoba . Tunggu tutorial selanjutnya๐๐
Jika ada yang ingin ditanyakan, silahkan kirim komentarnya dibawah ya๐