Sie sind auf Seite 1von 13

APLIKASI BUKU TAMU MENGGUNAKAN CODEIGNITER 2.1.

3
Ardyanto Hermawan

April 27

2013
Modul I

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


I. Hal-hal yang perlu dipersiapkan: 1. PHP: Codeigniter 2.1.3 2. Notepad++ 3. XAMPP 1.8.1 Mengubah file pada /application/config/: a. Config.php
$config['base_url'] = 'http://localhost/CodeIgniter/';

II.

b. Autoload.php
$autoload['libraries'] = array('database'); $autoload['helper'] = array('url','form');

c. Database.php
$db['default']['hostname'] $db['default']['username'] $db['default']['password'] $db['default']['database'] $db['default']['dbdriver'] = = = = = 'localhost'; 'root'; ''; 'codeigniter; 'mysql';

d. Routes.php
$route['default_controller'] = "buku_tamu";

III.

Membuat database dengan nama codeigniter a. Membuat table buku_tamu


CREATE table buku_tamu ( id tinyint(4) auto_increment, pengirim varchar(50), judul varchar(50), pesan text, waktu timestamp, primary key(id) );

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


IV. Membuat Model, View, dan Controller: 1. Membuat buku_tamu_model.php:
<?php class Buku_tamu_model extends CI_Model { public function get_all() { $query = $this->db->get('buku_tamu'); return $query->result_array(); } public function get_one($id) { $this->db->where('id',$id); $query = $this->db->get('buku_tamu'); if ($query->num_rows() == 1) { return $query->row_array(); } else { return $query->result_array(); } } public function insert($data) { $this->db->insert('buku_tamu',$data); } public function update($id,$data) { $this->db->where('id',$id); $this->db->update('buku_tamu',$data); } public function delete($id) { $this->db->where('id',$id); $this->db->delete('buku_tamu'); } }

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


2. Membuat view: a. index.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title><?php if (isset($title)){ echo $title; } ?></title> <meta name="description" content="Buku Tamu"> <meta name="author" content="Ardyanto Hermawan"> <link href="css/style.css" rel="stylesheet" media="screen"> <style type="text/css"> body{ font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; font-size:12px; } p, h1, form, button{border:0; margin:0; padding:0;} .spacer{clear:both; height:1px;} /* ----------- Wrapper ----------- */ .wrapper{ width: 960px; } /* ----------- My Form ----------- */ .myform{ margin:0 auto; width:400px; padding:14px; } /* ----------- stylized ----------- */ #stylized{ border:solid 2px #b7ddf2; background:#ebf4fb; } #stylized h1 { font-size:14px; font-weight:bold; margin-bottom:8px; } #stylized p{ font-size:11px; color:#666666; margin-bottom:20px; border-bottom:solid 1px #b7ddf2; padding-bottom:10px; } #stylized label{ display:block; font-weight:bold;

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


text-align:right; width:140px; float:left; } #stylized .small{ color:#666666; display:block; font-size:11px; font-weight:normal; text-align:right; width:140px; } #stylized input, textarea{ float:left; font-size:12px; padding:4px 2px; border:solid 1px #aacfe4; width:200px; margin:2px 0 20px 10px; } #stylized button{ clear:both; margin-left:150px; width:125px; height:31px; background:#666666; text-align:center; line-height:31px; color:#FFFFFF; font-size:11px; font-weight:bold; } #table table{ margin:10px auto; } </style> </head> <body> <div class="wrapper"> <div id="stylized" class="myform"> <?php echo form_open('buku_tamu/add'); ?> <h1>Silahkan isi form di bawah ini</h1> <p>Buku Tamu Menggunakan CodeIgniter 2.1.3</p> <label>Pengirim <span class="small">Masukkan nama</span> </label> <input type="text" name="pengirim" id="name" /> <label>Judul <span class="small">Masukkan judul pesan</span> </label> <input type="text" name="judul" id="email" /> <label>Pesan <span class="small">Masukkan pesan</span>

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


</label> <textarea name="pesan"></textarea> <button type="submit">Kirim</button> <div class="spacer"></div> <?php echo form_close(); ?> </div> <div id="table"> <?php if (isset($lists)): ?> <table border="1"> <thead> <tr> <th>No.</th> <th>Pengirim</th> <th>Judul</th> <th>Pesan</th> <th>Waktu</th> <th>Aksi</th> </tr> </thead> <tbody> <?php foreach ($lists as $row): ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['pengirim']; ?></td> <td><?php echo $row['judul']; ?></td> <td><?php echo $row['pesan']; ?></td> <td><?php echo $row['waktu'] ?></td> <td><?php echo anchor('buku_tamu/update/'.$row['id'],'Ubah'); ?> | <?php echo anchor('buku_tamu/delete/'.$row['id'],'Hapus'); ?></td> </tr> <?php endforeach ?> </tbody> </table> <?php else: ?> <h3>Data tidak ditemukan.</h3> <?php endif ?> </div> </div> </body> </html>

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


b. update.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title><?php if (isset($title)){ echo $title; } ?></title> <meta name="description" content="Buku Tamu"> <meta name="author" content="Ardyanto Hermawan"> <link href="css/style.css" rel="stylesheet" media="screen"> <style type="text/css"> body{ font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; font-size:12px; } p, h1, form, button{border:0; margin:0; padding:0;} .spacer{clear:both; height:1px;} /* ----------- My Form ----------- */ .wrapper{ width: 960px; } .myform{ margin:0 auto; width:400px; padding:14px; } /* ----------- stylized ----------- */ #stylized{ border:solid 2px #b7ddf2; background:#ebf4fb; } #stylized h1 { font-size:14px; font-weight:bold; margin-bottom:8px; } #stylized p{ font-size:11px; color:#666666; margin-bottom:20px; border-bottom:solid 1px #b7ddf2; padding-bottom:10px; } #stylized label{ display:block; font-weight:bold; text-align:right; width:140px; float:left;

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


} #stylized .small{ color:#666666; display:block; font-size:11px; font-weight:normal; text-align:right; width:140px; } #stylized input, textarea{ float:left; font-size:12px; padding:4px 2px; border:solid 1px #aacfe4; width:200px; margin:2px 0 20px 10px; } #stylized button{ clear:both; margin-left:150px; width:125px; height:31px; background:#666666; text-align:center; line-height:31px; color:#FFFFFF; font-size:11px; font-weight:bold; } </style> </head> <body> <div class="wrapper"> <div id="stylized" class="myform"> <?php echo form_open('buku_tamu/update/'.$isian['id']); ?> <h1>Silahkan update form di bawah ini</h1> <p>Buku Tamu Menggunakan CodeIgniter 2.1.3</p> <label>Pengirim <span class="small">Masukkan nama</span> </label> <input type="text" name="pengirim" id="pengirim" value="<?php echo $isian['pengirim']; ?>"/> <label>Judul <span class="small">Masukkan judul pesan</span> </label> <input type="text" name="judul" id="judul" value="<?php echo $isian['judul']; ?>" /> <label>Pesan <span class="small">Masukkan pesan</span> </label> <textarea name="pesan"><?php echo $isian['pesan']; ?></textarea> <button type="submit">Kirim</button>

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


<div class="spacer"></div> <?php echo form_close(); ?> </div> </div> </body> </html>

3. membuat controller buku_tamu.php


<?php class Buku_tamu extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('buku_tamu_model'); } function index() { $data['title'] = 'Daftar Tamu'; $data['lists'] = $this->buku_tamu_model->get_all(); $this->load->view('index.php', $data); } function add() { if ($this->input->post('judul') != '' AND $this>input->post('pesan') != '') { $data = array( 'pengirim' => $this->input->post('pengirim'), 'judul' => $this->input->post('judul'), 'pesan' => $this->input->post('pesan') ); $this->buku_tamu_model->insert($data); redirect('buku_tamu'); } else { redirect('buku_tamu'); } } function update($id) { if ($this->input->post('judul') != '' AND $this>input->post('pesan') != '') { $data = array( 'pengirim' => $this->input->post('pengirim'),

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


'judul' => $this->input->post('judul'), 'pesan' => $this->input->post('pesan') ); $this->buku_tamu_model->update($id,$data); redirect('buku_tamu'); } else { $data['title'] = 'Ubah komentar'; $data['isian'] = $this->buku_tamu_model>get_one($id); $this->load->view('update.php',$data); } } function delete($id) { if ($id != '') { $this->buku_tamu_model->delete($id); redirect('buku_tamu'); } else { redirect('buku_tamu'); } } }

10

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


V. Hasil: a. Tampilan awal

b. Mengisikan data pertama

11

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


c. Hasil masukkan data pertama

d. Mengubah data percobaan pertama

12

APLIKASI BUKU TAMU MENGGUNAKAN 2013 CODEIGNITER 2.1.3


e. Hasil perubahan

f. Menghapus data

13

Das könnte Ihnen auch gefallen