SOAL 1
a) Membuat dan Mengonfigurasi Database
Buat database baru dengan nama “blog” dan sebuah tabel dengan nama “post” dengan struktur
berikut.
b) Screenshot
application/config/database.php
c) Syntax
1 $db['default'] = array(
2 'dsn' => '',
3 'hostname' => 'localhost:3307',
4 'username' => 'root',
5 'password' => '',
6 'database' => 'blog',
7 'dbdriver' => 'mysqli',
8 'dbprefix' => '',
9 'pconnect' => FALSE,
10 'db_debug' => (ENVIRONMENT !== 'production'),
11 'cache_on' => FALSE,
12 'cachedir' => '',
13 'char_set' => 'utf8',
14 'dbcollat' => 'utf8_general_ci',
15 'swap_pre' => '',
16 'encrypt' => FALSE,
17 'compress' => FALSE,
18 'stricton' => FALSE,
19 'failover' => array(),
20 'save_queries' => TRUE
21 );
d) Penjelasan
Langkah di atas dilakukan untuk mengonfigurasi username, password, dan database yang akan
digunakan oleh server.
SOAL 2
a) Menampilkan Artikel
Buat sebuah file baru yaitu “application/models/Blog_model.php” dan tulis kode berikut.
Buat controller baru pada file “application/controllers/Blog.php” dan tulis kode berikut.
Kemudian buat satu file baru yaitu “application/views/read.php” dengan kode berikut.
Kemudian buka URL “http://localhost/ci/blog/read” di browser. Jelaskan alur eksekusi kode ketiga
kode di atas.
b) Screenshot
Isi database
http://localhost/ci/blog/read
c) Syntax
1 //Blog_model.php
2 <?php
3 class Blog_model extends CI_Model
4 {
5 public function get_posts()
6 {
7 $this->load->database();
8 $query = $this->db->query("SELECT * FROM post");
9 return $query->result();
}
}
1 //Blog.php
2 <?php
3 class Blog extends CI_Controller
4 {
5 public function read()
6 {
7 $this->load->model('Blog_model');
8 $posts = $this->Blog_model->get_posts();
9 $data['posts'] = $posts;
10 $this->load->view('read', $data);
11 }
12 }
1 //read.php
2 <!DOCTYPE html>
3 <html>
4
5 <head>
6 <title>Blog Saya</title>
7 </head>
8
9 <body>
10 <h1>Daftar Artikel</h1>
11 <?php foreach ($posts as $post): ?>
12 <h2>
13 <?php echo $post->judul; ?>
14 </h2>
15 <p>
16 <?php echo $post->konten; ?>
17 </p>
18 <?php endforeach; ?>
19 </body>
20
21 </html>
d) Penjelasan
Buat form untuk membuat artikel blog seperti berikut pada file “application/views/blog_create.php”.
b) Screenshot
http://localhost/ci/blog/create
c) Syntax
1 //blog_create.php
2
3 <!DOCTYPE html>
4 <html>
5
6 <head>
7 <title>Blog Saya</title>
8 </head>
9
10 <body>
11 <h1>Tulis Artikel Baru</h1>
12 <form action="<?php echo base_url("blog / create_process"); ?>"
13 method="post">
14 <label>
15 Judul: <input type="text" name="judul" autofocus>
16 </label>
17 <br>
18 <label>
19 Konten:<br>
20 <textarea name="konten"></textarea>
21 </label>
22 <br>
23 <input type="submit" value="Simpan">
24 </form>
25 </body>
26
27 </html>
1 //Blog.php
2 <?php
3 class Blog extends CI_Controller
4 {
5 public function read()
6 {
7 $this->load->model('Blog_model');
8 $posts = $this->Blog_model->get_posts();
9 $data['posts'] = $posts;
10 $this->load->view('read', $data);
11 }
12 public function create()
13 {
14 $this->load->helper('url');
15 $this->load->view("blog_create");
16 }
17 public function create_process()
18 {
19 $judul = $this->input->post("judul");
20 $konten = $this->input->post("konten");
21 $this->load->helper('url');
22 $this->load->model("Blog_model");
23 $this->Blog_model->insert_post($judul, $konten);
24 redirect(base_url(), "refresh");
25 }
26 }
1 //Blog_model.php
2 <?php
3 class Blog_model extends CI_Model
4 {
5 public function get_posts()
6 {
7 $this->load->database();
8 $query = $this->db->query("SELECT * FROM post");
9 return $query->result();
10 }
11 public function insert_post($judul, $konten)
12 {
13 $this->load->database();
14 $this->db->query("INSERT INTO post (judul, konten) VALUES
15 ('$judul',
16 '$konten')");
17 }
18 }
d) Penjelasan
SOAL 4
a) Menghapus Artikel
Hapus satu artikel dengan mengklik tombol “Hapus”. Beri penjelasan tentang kode yang Anda buat di
atas.
b) Screenshot
http://localhost/ci/blog/read
c) Syntax
1 //read.php
2 <!DOCTYPE html>
3 <html>
4
5 <head>
6 <title>Blog Saya</title>
7 </head>
8
9 <body>
10 <h1>Daftar Artikel</h1>
11 <?php foreach ($posts as $post): ?>
12 <h2>
13 <?php echo $post->judul; ?>
14 </h2>
15 <a href="<?php echo base_url('blog/delete/' . $post->id);
16 ?>">Hapus</a>
17 <p>
18 <?php echo $post->konten; ?>
19 </p>
20 <?php endforeach; ?>
21 </body>
22
23 </html>
1 //Blog.php
2 <?php
3 class Blog extends CI_Controller
4 {
5 public function read()
6 {
7 $this->load->helper('url');
8 $this->load->model('Blog_model');
9 $posts = $this->Blog_model->get_posts();
10 $data['posts'] = $posts;
11 $this->load->view('read', $data);
12 }
13 public function create()
14 {
15 $this->load->helper('url');
16 $this->load->view("blog_create");
17 }
18 public function create_process()
19 {
20 $judul = $this->input->post("judul");
21 $konten = $this->input->post("konten");
22 $this->load->helper('url');
23 $this->load->model("Blog_model");
24 $this->Blog_model->insert_post($judul, $konten);
25 redirect(base_url(), "refresh");
26 }
27 public function delete($id)
28 {
29 $this->load->helper('url');
30 $this->load->model('Blog_model');
31 $this->Blog_model->delete_post($id);
32 redirect(base_url(), 'refresh');
33 }
34 }
1 //Blog_model.php
2 <?php
3 class Blog_model extends CI_Model
4 {
5 public function get_posts()
6 {
7 $this->load->database();
8 $query = $this->db->query("SELECT * FROM post");
9 return $query->result();
10 }
11 public function insert_post($judul, $konten)
12 {
13 $this->load->database();
14 $this->db->query("INSERT INTO post (judul, konten) VALUES
15 ('$judul',
16 '$konten')");
17 }
18 public function delete_post($id)
19 {
20 $this->load->database();
21 $this->db->query("DELETE FROM post WHERE id = $id");
22 }
23 }
d) Penjelasan
Ketika tombol "Hapus" pada halaman read.php diklik, method delete() pada controller Blog akan
dieksekusi. Method ini akan mengambil parameter $id yang merupakan id artikel yang akan
dihapus. Di dalam method tersebut, model Blog_model akan dimuat dan method delete_post()
akan dipanggil untuk menghapus artikel berdasarkan id yang diberikan. Setelah penghapusan
berhasil dilakukan, halaman akan dimuat ulang dan artikel yang telah dihapus tidak akan lagi
ditampilkan dalam daftar artikel.
SOAL 5
a) Mekanisme Update
b) Screenshot
1 //Blog.php
2 <?php
3 class Blog extends CI_Controller
4 {
5 public function read()
6 {
7 $this->load->helper('url');
8 $this->load->model('Blog_model');
9 $posts = $this->Blog_model->get_posts();
10 $data['posts'] = $posts;
11 $this->load->view('read', $data);
12 }
13 public function create()
14 {
15 $this->load->helper('url');
16 $this->load->view("blog_create");
17 }
18 public function create_process()
19 {
20 $judul = $this->input->post("judul");
21 $konten = $this->input->post("konten");
22 $this->load->helper('url');
23 $this->load->model("Blog_model");
24 $this->Blog_model->insert_post($judul, $konten);
25 redirect(base_url(), "refresh");
26 }
27 public function delete($id)
28 {
29 $this->load->helper('url');
30 $this->load->model('Blog_model');
31 $this->Blog_model->delete_post($id);
32 redirect(base_url(), 'refresh');
33 }
34 public function edit($id)
35 {
36 $this->load->helper('url');
37 $this->load->model('Blog_model');
38 $post = $this->Blog_model->get_post($id);
39 $data['post'] = $post;
40 $this->load->view('edit', $data);
41 }
42 public function update($id)
43 {
44 $this->load->helper('url');
45 $this->load->model('Blog_model');
46 $judul = $this->input->post('judul');
47 $konten = $this->input->post('konten');
48 $this->Blog_model->update_post($id, $judul, $konten);
49 redirect(base_url(), 'refresh');
50 }
51
52 }
1 //Blog_model.php
2 <?php
3 class Blog_model extends CI_Model
4 {
5 public function get_posts()
6 {
7 $this->load->database();
8 $query = $this->db->query("SELECT * FROM post");
9 return $query->result();
10 }
11 public function get_post($id)
12 {
13 $this->load->database();
14 $query = $this->db->query("SELECT * FROM post WHERE id =
15 $id");
16 return $query->row();
17 }
18
19 public function insert_post($judul, $konten)
20 {
21 $this->load->database();
22 $this->db->query("INSERT INTO post (judul, konten) VALUES
23 ('$judul',
24 '$konten')");
25 }
26 public function delete_post($id)
27 {
28 $this->load->database();
29 $this->db->query("DELETE FROM post WHERE id = $id");
30 }
31 public function update_post($id, $judul, $konten)
32 {
33 $this->load->database();
34 $this->db->set('judul', $judul);
35 $this->db->set('konten', $konten);
$this->db->where('id', $id);
36
$this->db->update('post');
37 }
38
39 }
1 //edit.php
2 <!DOCTYPE html>
3 <html lang="en">
4
5 <head>
6 <title>Blog Saya - Edit Artikel</title>
7 </head>
8
9 <body>
10 <h1>Edit Artikel</h1>
11 <form action="<?php echo base_url('blog/update/' . $post->id);
12 ?>" method="post">
13 <label>
14 Judul: <input type="text" name="judul" value="<?php echo
15 $post->judul; ?>" autofocus>
16 </label>
17 <br>
18 <label>
19 Konten:<br>
20 <textarea name="konten"><?php echo $post->konten;
21 ?></textarea>
22 </label>
23 <br>
24 <input type="submit" value="Simpan">
25 </form>
26 </body>
27
28 </html>
1 //read.php
2 <!DOCTYPE html>
3 <html>
4
5 <head>
6 <title>Blog Saya</title>
7 </head>
8
9 <body>
10 <h1>Daftar Artikel</h1>
11 <?php foreach ($posts as $post): ?>
12 <h2>
13 <?php echo $post->judul; ?>
14 </h2>
15 <a href="<?php echo base_url('blog/delete/' . $post->id);
16 ?>">Hapus</a>
17 <a href="<?php echo base_url('blog/edit/' . $post->id);
18 ?>">Edit</a>
19 <p>
20 <?php echo $post->konten; ?>
21 </p>
22 <?php endforeach; ?>
23 </body>
24
25 </html>
d) Penjelasan
Dalam proses penambahan fitur update, terdapat beberapa langkah yang dilakukan. Pertama, dalam
controller Blog, method edit($id) ditambahkan untuk memuat data artikel berdasarkan id
menggunakan model Blog_model. Kemudian, dibuatlah view edit.php yang menampilkan form edit
artikel dengan nilai awal yang diambil dari data artikel yang diperoleh. Setelah itu, ketika form
disimpan, pengguna akan diarahkan ke method update($id) pada controller Blog dengan
menggunakan aksi URL yang sesuai. Terakhir, di dalam method update($id), data yang diubah dari
input form diambil dan method update_post($id, $judul, $konten) pada model Blog_model
dipanggil untuk memperbarui artikel yang sesuai dengan id yang diberikan.
SOAL 6
a) Kelas Query Builder
Ubah semua perintah database (SELECT, INSERT, UPDATE, dan DELETE) pada komponen model
menggunakan fungsi-fungsi pada kelas Query Builder. Baca dokumentasi framework CodeIgniter di
https://www.codeigniter.com/user_guide/database/query_builder.html)
b) Screenshot
c) Syntax
1 //Blog_model.php
2 <?php
3 class Blog_model extends CI_Model
4 {
5 public function get_posts()
6 {
7 $this->load->database();
8 $this->db->select('*');
9 $this->db->from('post');
10 $query = $this->db->get();
11 return $query->result();
12 }
13
14 public function get_post($id)
15 {
16 $this->load->database();
17 $this->db->select('*');
18 $this->db->from('post');
19 $this->db->where('id', $id);
20 $query = $this->db->get();
21 return $query->row();
22 }
23
24 public function insert_post($judul, $konten)
25 {
26 $this->load->database();
27 $data = array(
28 'judul' => $judul,
29 'konten' => $konten
30 );
31 $this->db->insert('post', $data);
32 }
33
34 public function delete_post($id)
35 {
36 $this->load->database();
37 $this->db->where('id', $id);
38 $this->db->delete('post');
39 }
40
41 public function update_post($id, $judul, $konten)
42 {
43 $this->load->database();
$this->db->set('judul', $judul);
44
$this->db->set('konten', $konten);
45
$this->db->where('id', $id);
46
$this->db->update('post');
47
}
48
49
}
50
d) Penjelasan
Dalam kode yang diatas, perintah-perintah database diubah menggunakan fungsi-fungsi dari
kelas Query Builder pada framework CodeIgniter. Misalnya, untuk perintah SELECT,
digunakan $this->db->select('*') dan $this->db->from('post') untuk memilih semua kolom dari
tabel 'post'. Untuk perintah INSERT, data yang akan dimasukkan ke dalam tabel 'post' diwakili
oleh array $data, dan digunakan $this->db->insert('post', $data) untuk menyisipkan data tersebut
ke dalam tabel. Untuk perintah DELETE, menggunakan $this->db->where('id', $id) dan
$this->db->delete('post') untuk menghapus baris yang memiliki ID tertentu dari tabel 'post'. Dan
utuk perintah UPDATE, digunakan $this->db->set('column', 'value') untuk mengatur nilai kolom
yang akan diperbarui, dan kemudian menggunakan $this->db->where('condition') untuk
menentukan kondisi baris yang akan diperbarui.