CREATE TABLE `buku` (
`kode_buku` varchar(20) primary key NOT NULL,
`nama_buku` varchar(50) NOT NULL,
`judul_buku` varchar(100) NOT NULL,
`pengarang` varchar(50) NOT NULL,
`jumlah_halaman` int(11) NOT NULL,
`penerbit` varchar(100) NOT NULL,
`tahun_terbit` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
<!doctype html>
<html>
<head>
<meta charset=”utf-8″/>
<title><?php echo $judul; ?></title>
<style>
body{
font: 10px verdana
}
table{
width:100%; border:1px solid #ccc; align:left;
}
th{
background:blue; padding:3px; color:#fff;
}
td{
border:1px solid #ccc;
}
h1 {
font:20px verdana;
}
hr{
margin-bottom:20px;
}
h3 a{
font:10px verdana;
padding:5px;
background:red;
text-decoration:none;
color:#fff;
}
h3 a:hover{
background:green;
}
</style>
</head>
<body>
<h1>BUKU</h1>
<hr>
<h3><?php echo ‘<a href=”‘.base_url().’buku/add_buku”>Add Buku</a>’?></h3>
<table>
<tr>
<th align=”left”>Kode Buku</th>
<th align=”left”>Nama Buku</th>
<th align=”left”>Judul Buku</th>
<th align=”left”>Pengarang</th>
<th align=”left”>Jumlah Halaman</th>
<th align=”left”>Penerbit</th>
<th align=”left”>Tahun Terbit</th>
<th colspan=”2″>Aksi</th>
</tr>
<?php
foreach($databuku as $buku){
?>
<tr>
<td><?php echo $buku->kode_buku; ?></td>
<td><?php echo $buku->nama_buku; ?></td>
<td><?php echo $buku->judul_buku; ?></td>
<td><?php echo $buku->pengarang; ?></td>
<td align=”center”><?php echo $buku->jumlah_halaman; ?></td>
<td><?php echo $buku->penerbit; ?></td>
<td><?php echo $buku->tahun_terbit; ?></td>
<td><?php echo ‘<a href=”‘.base_url().’buku/delete_buku/’.$buku->kode_buku.'” onclick=”return confirm(‘Anda yakin akan menghapus JUDUL BUKU ‘.$buku->judul_buku.’?’)”>Delete</a>’?></td>
<td><?php echo ‘<a href=”‘.base_url().’buku/edit_buku/’.$buku->kode_buku.'”>Edit</a>’?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
class Buku_model extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_buku_all()
{
$query=$this->db->query(“SELECT * FROM buku ORDER BY kode_buku”);
return $query->result();
}
}
<?php
class Buku extends CI_Controller{
Public function __Construct()
{
parent ::__construct();
$this->load->helper(‘form’);
$this->load->helper(‘url’);
}
public function index()
{
redirect(‘buku/list_buku’);
}
public function list_buku()
{
$this->load->model(‘buku_model’);
$data[‘judul’] = ‘::DATA BUKU::’;
$data[‘databuku’] = $this->buku_model->get_buku_all();
$this->load->view(‘buku_view’, $data);
}
}
<!doctype html>
<html>
<head>
<title><?php echo $judul;?></title>
<style>
form{
font:10px verdana; width:340px;
margin:20px auto; border:1px solid #ccc;
padding:0;
}
label{
font-weight:bold; margin-left:20px;
}
.judul{
background:blue; padding:7px;
font:14px verdana; color:#fff;
border:1px solid blue;
}
input[type=”text”]{
margin-left:20px; width:300px;
border:1px solid #ccc; height:25px;
border-radius:5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
}
input[type=”submit”], input[type=”reset”]{
margin-top:10px; font:10px verdana;
background:red; color:#fff;
padding:5px; width:50px;
border:1px solid red; border-radius:3px;
-webkit-border-radius:3px;
-moz-border-radius:3px;
}
input[type=”submit”]{ margin-left:110px; }
input[type=”reset”]{ margin-left:20px; }
</style>
</head>
<body>
<form action=”simpan_buku” method=”post”>
<div class=”judul”>
:: TAMBAH BUKU ::
</div>
<p>
<label>Kode Buku</label><br />
<input name=”kodebuku” type=”text”/>
</p>
<p>
<label>Nama Buku</label><br />
<input type=”text” name=”namabuku”/>
<p>
<p>
<label>Judul Buku</label><br />
<input type=”text” name=”judulbuku”/>
</p>
<p>
<label>Pengarang</label><br />
<input name=”pengarang” type=”text”/>
</p>
<p>
<label>Jumlah Halaman</label><br />
<input type=”text” name=”jumlahhalaman”>
</p>
<p>
<label>Penerbit</label><br />
<input type=”text” name=”penerbit”>
</p>
<p>
<label>Tahun Terbit (yyyy-mm-dd)</label><br />
<input type=”text” name=”tahunterbit”>
</p>
<p>
<input type=”submit” value=”Simpan”/>
<input type=”reset” value=”Back” onclick=”self.history.back();”/>
</p>
</form>
</body>
</html>
public function simpan_buku()
{
$simpan_data=array(
‘kode_buku ‘ => $this->input->post(‘kodebuku’),
‘nama_buku’ => $this->input->post(‘namabuku’),
‘judul_buku’ => $this->input->post(‘judulbuku’),
‘pengarang’ => $this->input->post(‘pengarang’),
‘jumlah_halaman’ => $this->input->post(‘jumlahhalaman’),
‘penerbit’ => $this->input->post(‘penerbit’),
‘tahun_terbit’ => $this->input->post(‘tahunterbit’)
);
$simpan = $this->db->insert(‘buku’, $simpan_data);
return $simpan;
}
public function add_buku()
{
$data[‘judul’] = ‘::DATA BUKU::’;
$this->load->view(‘addbuku_view’, $data);
}
public function simpan_buku()
{
$this->load->model(‘buku_model’);
$this->buku_model->simpan_buku();
redirect(‘buku/index’);
}
public function delete_buku($kode_buku)
{
$this->load->model(‘buku_model’);
$kode_buku = $this->buku_model->delete_buku($kode_buku);
redirect(‘buku/index’);
}
• Pada file Model : buku_model.php, tambahkan coding berikut setelah public function simpan_buku() :
public function delete_buku($kode_buku)
{
$query=$this->db->query(“DELETE FROM buku WHERE kode_buku=’$kode_buku'”);
}
<!doctype html>
<html>
<head>
<title><?php echo $judul;?></title>
<style>
form{
font:10px verdana; width:340px;
margin:20px auto; border:1px solid #ccc;
padding:0;
}
label{
font-weight:bold; margin-left:20px;
}
.judul{
background:blue; padding:7px;
font:14px verdana; color:#fff;
border:1px solid blue;
}
input[type=”text”]{
margin-left:20px; width:300px;
border:1px solid #ccc;
height:25px;
border-radius:5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
}
input[type=”submit”], input[type=”reset”]{
margin-top:10px; font:10px verdana;
background:red; color:#fff;
padding:5px; width:50px;
border:1px solid red;
border-radius:3px;
-webkit-border-radius:3px;
-moz-border-radius:3px;
}
input[type=”submit”]{ margin-left:110px; }
input[type=”reset”]{ margin-left:20px; }
</style>
</head>
<body>
<?php
$att = array(‘id’ => ‘user-buku’);
echo form_open(‘buku/simpan_edit_buku’, $att);
echo form_hidden(‘kode_buku’,$edit->kode_buku);
?>
<form action=”simpan_buku” method=”post”>
<div class=”judul“> :: UPDATE BUKU ::</div>
<p>
<label>Kode Buku</label><br />
<input type=”text” name=”kodebuku” value=”<?php echo $edit->kode_buku; ?>” readonly/>
</p>
<p>
<label>Nama Buku</label><br />
<input type=”text” name=”namabuku” value=”<?php echo $edit->nama_buku; ?>”/>
<p>
<p>
<label>Judul Buku</label><br />
<input type=”text” name=”judulbuku” value=”<?php echo $edit->judul_buku; ?>”/>
</p>
<p>
<label>Pengarang</label><br />
<input type=”text” name=”pengarang” value=”<?php echo $edit->pengarang; ?>”/>
</p>
<p>
<label>Jumlah Halaman</label><br />
<input type=”text” name=”jumlahhalaman” value=”<?php echo $edit->jumlah_halaman; ?>”/>
</p>
<p>
<label>Penerbit</label><br />
<input type=”text” name=”penerbit” value=”<?php echo $edit->penerbit; ?>”/>
</p>
<p>
<label>Tahun Terbit (yyyy-mm-dd)</label><br />
<input type=”text” name=”tahunterbit” value=”<?php echo $edit->tahun_terbit; ?>”/>
</p>
<p>
<input type=”submit” value=”Update”/>
<input type=”reset” value=”Back” onclick=”self.history.back();”/>
</p>
</form>
</body>
</html>
public function edit_buku($kode_buku)
{
$data[‘judul’] = ‘::DATA BUKU::’;
$this->load->model(‘buku_model’);
$data[‘edit’]=$this->buku_model->edit_buku($kode_buku);
$this->load->view(‘editbuku_view’, $data);
}
public function simpan_edit_buku()
{
$kode_buku = $this->input->post(‘kodebuku’);
$nama_buku = $this->input->post(‘namabuku’);
$judul_buku = $this->input->post(‘judulbuku’);
$pengarang = $this->input->post(‘pengarang’);
$jumlah_halaman = $this->input->post(‘jumlahhalaman’);
$penerbit = $this->input->post(‘penerbit’);
$tahun_terbit = $this->input->post(‘tahunterbit’);
$data[‘judul’] = ‘::DATA BUKU::’;
$this->load->model(‘buku_model’);
$data[‘edit’] = $this->buku_model->simpan_edit_buku($kode_buku, $nama_buku, $judul_buku, $pengarang, $jumlah_halaman, $penerbit, $tahun_terbit);
redirect(‘buku/index’);
}
• Pada file Model : buku_model.php, tambahkan coding berikut setelah public function delete_buku($kode_buku)
public function edit_buku($kode_buku)
{
$q=”SELECT * FROM buku WHERE kode_buku=’$kode_buku'”;
$query=$this->db->query($q);
return $query->row();
}
public function simpan_edit_buku($kode_buku, $nama_buku, $judul_buku, $pengarang, $jumlah_halaman, $penerbit, $tahun_terbit)
{
$data = array(
‘kode_buku’ => $kode_buku,
‘nama_buku’ => $nama_buku,
‘judul_buku’ => $judul_buku,
‘pengarang’ => $pengarang,
‘jumlah_halaman’ => $jumlah_halaman,
‘penerbit’ => $penerbit,
‘tahun_terbit’ => $tahun_terbit
);
$this->db->where(‘kode_buku’, $kode_buku);
$this->db->update(‘buku’, $data);
}
lintasme.init(‘right’); // options : left, top, bottom, right
Source: New feed