Kamis, 11 Juni 2015

Membuat Database "Perpustakaan"

Berikut ini merupakan interface untuk mengelola data perpustakaan yang terdiri dari daftar buku, penerbit, pengarang dan bahasabahasa dengan fungsi insert,update,delete,view

1. DDL DATABASE “perpustakaan”

Syntak SQL membuat Database
CREATE DATABASE perpustakaan



CREATE TABLE `buku` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `no_buku` varchar(50) NOT NULL,
  `judul` text NOT NULL,
  `edisi` varchar(100) NOT NULL,
  `tahun_terbit` year(4) NOT NULL,
  `penerbit_id` int(11) NOT NULL,
  `bahasa_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `penerbit_id` (`penerbit_id`),
  KEY `bahasa_id` (`bahasa_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1
Syntak SQL membuat tabel Buku dan kunci kandidat
CREATE TABLE `bahasa` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `kode` varchar(4) NOT NULL,
  `nama` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Membuat View pada SQLYog
Vbuku


DELIMITER $$
USE `perpustakaan`$$
DROP VIEW IF EXISTS `vbuku`$$
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vbuku` AS
SELECT
  `b`.`id`                    AS `id`,
  `b`.`no_buku`        AS `no_buku`,
  `b`.`judul`               AS `judul`,
  `b`.`edisi`                  AS `edisi`,
  `b`.`tahun_terbit`  AS `tahun_terbit`,
  `t`.`nama`               AS `penerbit`,
  `t`.`kota`                  AS `kota`,
  `bhs`.`nama`           AS `bahasa`,
  GROUP_CONCAT(`p`.`nama` SEPARATOR '|') AS `pengarang`
FROM ((((`buku` `b`
     LEFT JOIN `penerbit` `t`
       ON ((`t`.`id` = `b`.`penerbit_id`)))
       LEFT JOIN `bahasa` `bhs`
     ON ((`bhs`.`id` = `b`.`bahasa_id`)))
    LEFT JOIN `buku_pengarang` `bp`
      ON ((`bp`.`buku_id` = `b`.`id`)))
   LEFT JOIN `pengarang` `p`
     ON ((`p`.`id` = `bp`.`pengarang_id`)))

GROUP BY `b`.`id`$$

DELIMITER ;



2.     Script cakePHP


Menggunakan Cake PHP versi 2, letakkan folder di xampp -> htdocs kemudian rename menjadi nama"perpustakaan"

Membuat Model 
Terletak di perpustakaan - app -model bikin model degan nama sebagai berikut :

Bahasa.php
<?php
                class Bahasa extends AppModel {
                                var $nama='Bahasa';
                                var $useTable='bahasa';
                                var $hasAndBelongsToMany=array('Buku');
                                var $displayField='nama';
                               
                               
                }
?>


Pengarang.php
<?php
                class Pengarang extends AppModel {
                                var $nama='Pengarang';
                                var $useTable='pengarang';
                                var $displayField='nama';
                                var $hasAndBelongsToMany=array('Buku');
                               
                }
?>



Penerbit.php
<?php
                class Penerbit extends AppModel{
                                var $name='Penerbit';
                                var $useTable='penerbit';
                                var $hasMany=array ('buku');
                                var $displayField='nama';
                }
?>




Vbuku.php
<?php
                class Vbuku extends AppModel{
                                var $name='Vbuku';
                                var $useTable='vbuku';
                                }
?>


Membuat Controller
Terletak di perpustakaan - app -controller bikin controller dengan nama sebagai berikut :
BahasasController.php
 

<?php
                class BahasasController extends AppController {
                var $name='Bahasas';
                var $uses=array('Bahasa');
                var $scaffold;
                }
?>







PenerbitsController.php
<?php
                class BahasasController extends AppController {
                var $name='Bahasas';
                var $uses=array('Bahasa');
                var $scaffold;
                }
?>

BukusController.php
<?php
                class BukusController extends AppController {
                                var $name='Bukus';
                                var $uses = array('Vbuku','Buku','Penerbit','Pengarang','Bahasa');
                               
                                function index(){
                                                $data = $this->Vbuku->find('all');
                                                $this->set('buku', $data);
                                }
                               
                                function add() {
                                                if (!empty($this->data)) {
                                                                if ($this->Buku->save($this->data)) {
                                                                                $this->Session->setFlash('Buku telah disimpan');
                                                                                $this->redirect(array('action'=>'index'), null, true);
                                                                } else {
                                                                                $this->Session->setFlash('buku belum disimpan.Coba lagi!.');
                                                                }
                                                }
                                                $penerbit = $this->Penerbit->find('list', array());              
                                                $this->set('penerbit', $penerbit);
                                               
                                                $pengarang = $this->Pengarang->find('list', array());
                                                $this->set('pengarangs', $pengarang);
                                               
                                                $bahasa = $this->Bahasa->find('list', array());
                                                $this->set('bahasa', $bahasa);
                                }
                               
                                function edit($id=null) {
                                                if (!empty($this->data)) {
                                                                if ($this->Buku->save($this->data)) {
                                                                                $this->Session->setFlash('Buku telah disimpan');
                                                                                $this->redirect(array('action'=>'index'), null, true);
                                                                } else {
                                                                                $this->Session->setFlash('buku belum disimpan.Coba lagi!.');
                                                                }
                                                }else {
                                                                $this->data = $this->Buku->findById($id);
                                                }
                                                $penerbit = $this->Penerbit->find('list', array());              
                                                $this->set('penerbit', $penerbit);
                                               
                                                $pengarang = $this->Pengarang->find('list', array());
                                                $this->set('pengarangs', $pengarang);
                                               
                                                $bahasa = $this->Bahasa->find('list', array());
                                                $this->set('bahasa', $bahasa);
                                }
                               
                                function delete($id =null) {
                                                if (!$id) {
                                                                $this->Session->setFlash('id Buku tidak valid');
                                                                $this->redirect(array('action'=>'index'), null, true);
                                                }
                                                if ($this->Buku->delete($id)) {
                                                                $this->Session->setFlash('Buku #'.$id.' dihapus.');
                                                                $this->redirect(array('action'=>'index'), null, true);
                                                }
                                }
                }
?>






Membuat View 
Terletak di perpustakaan - app -view bikin view dengan nama sebagai berikut :
Index.ctp

<h2>Daftar Buku</h2>
<?php echo $this->Html->link('Tambah Buku',array('action'=>'add')); ?>
<?php if(empty($buku)): ?>
                <p>tidak ada daftar buku</p>
<?php else: ?>
<br/>
<table>
                <tr>
                                <th>Judul</th>
                                <th>Pengarang</th>
                                <th>No.Buku</th>
                                <th>Tahun</th>
                                <th>Edisi</th>
                                <th>Penerbit</th>
                                <th>Kota Penerbit</th>
                                <th>Bahasa</th>
                                <th>Aksi</th>
                </tr>
                <?php foreach ($buku as $buku): ?>
                <tr>
                                <td><?php echo $buku['Vbuku']['judul'] ?></td>
                                <td><?php echo $buku['Vbuku']['pengarang'] ?></td>
                                <td><?php echo $buku['Vbuku']['no_buku'] ?></td>
                                <td><?php echo $buku['Vbuku']['tahun_terbit'] ?></td>
                                <td><?php echo $buku['Vbuku']['edisi'] ?></td>
                                <td><?php echo $buku['Vbuku']['penerbit'] ?></td>
                                <td><?php echo $buku['Vbuku']['kota'] ?></td>
                                <td><?php echo $buku['Vbuku']['bahasa'] ?></td>
                                <td>
                                                <?php echo $this->Html->link('Edit', array('action'=>'edit',$buku['Vbuku']['id'])); ?>
                                               
                                                <?php echo $this->Html->link('Delete', array('action'=>'delete',
$buku['Vbuku']['id']), null, 'Anda yakin akan menghapus buku ini?'); ?>
                                </td>
                </tr>
                <?php endforeach; ?>
</table>
<?php endif;
?>


add.ctp
<?php echo $this->Form->create('Buku',array('url'=>'add'));?>
<fieldset>
<legend>Tambah Buku</legend>
<?php
echo $this->Form->input('judul');
echo $this->Form->input('Pengarang',array( 'type' =>
'select', 'multiple' => 'checkbox' ));
echo $this->Form->input('no_buku');
echo $this->Form->input('tahun_terbit');
echo $this->Form->input('edisi');
echo $this->Form->input('penerbit_id', array('div' => 'typeselect','options' => $penerbit,'empty' => false));
echo $this->Form->input('bahasa_id', array('div' => 'typeselect','options' => $bahasa,'empty' => false));
?>
</fieldset>
<?php echo $this->Form->end('Simpan');?>
<?php echo $this->Html->link('Daftar semua buku',array('action'=>'index')); ?>$buku['Vbuku']['id']), null, 'Anda yakin akan menghapus buku ini?'); ?>
                                </td>
                </tr>
                <?php endforeach; ?>
</table>
<?php endif;
?>

edit.ctp
<?php echo $this->Form->create('Buku',array('url'=>'edit'));?>
<fieldset>
<legend>Ubah Buku</legend>
<?php
echo $this->Form->hidden('id');
echo $this->Form->input('judul');
echo $this->Form->input('Pengarang',array( 'type' =>
'select', 'multiple' => 'checkbox' ));
echo $this->Form->input('no_buku');
echo $this->Form->input('tahun_terbit');
echo $this->Form->input('edisi');
echo $this->Form->input('penerbit_id', array('div' => 'typeselect','options' => $penerbit,'empty' => false));
echo $this->Form->input('bahasa_id', array('div' => 'typeselect','options' => $bahasa,'empty' => false));
?>
</fieldset>
<?php echo $this->Form->end('Simpan');?>
<?php echo $this->Html->link('Daftar semua buku',array('action'=>'index')); ?>

Hasilnya: 

Gambar 1.1. CakePHP 2.6.2 telah aktif
 
Gambar 1.2. localhost/perpus/Bahasas
 Tampilan awal



Gambar 1.3. Mengedit data di tabel bahasa. Pilih Aksi Edit


Hasilnya ketika sudah mengedit data:



Gambar 1.4. menghapus data Pilih Aksi Delete
Hasilnya ketika data sudah dihapus


Gambar 1.5. Menambah/mengisi data di tabel bahasa. Pilih Tambah Buku


Hasilnya ketika sudah memasukkan data:


  Alhamdulilah, kelar jugaa ^.^

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes Powered by Blogger | DSW printable coupons