Tuesday, April 7, 2015

Operasi database select dan delete dengan konsep model view controller codeigniter



Setelah mempelajari sekilas tentang Model , View dan Controller pada CodeIgniter, pada pembahasan kali ini kita coba untuk melakukan operasi database dasar dengan menggunakan perintah select dan delete pada CodeIgniter dan database MySQL.
Untuk mempermudah langkah - langkah belajar, kita gunakan struktur tabel seperti pada postingan sebelumnya yang dapat anda download di sini. Pada langkah kali ini, kita akan menampilkan dterlebih dahulu daftar postingan dalam bentuk tabel html, kemudian pada halaman tersebut terdapat link untuk melakukan pembacaan dan link untuk menghapus data.
Langkah - langkah yang harus kita lakukan adalah sebagai berikut :
1. Buat controller home
Pada contoh kali ini, kami sudah melakukan modifikasi contoh controller seperti yang telah kita bahas sebelumnya, silahkan perhatikan contoh script dibawah ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Home extends CI_Controller {
 
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/home
     *  - or - 
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/home/<method_name>
     */
    public function index()
    {
        echo 'Selamat datang di CodeIgniter';
    }
     
    function posting()
    {
        $this->load->model('Webmodel');
        $data['posting'] = $this->Webmodel->list_posting();
        $this->load->view('posting',$data);
    }
     
    function baca($id=false)
    {
        $this->load->model('Webmodel');
        if($id){
            $data['baca'] = $this->Webmodel->baca_posting($id);
        }
        else{
            $data['baca'] = 'Tidak ada data untuk ditampilkan';
        }
        $this->load->view('baca_posting',$data);
    }
     
    function hapus($id=false)
    {
        $this->load->model('Webmodel');
        if($id){
            $this->Webmodel->hapus_posting($id);
        }
        //setelah menghapus kembalikan ke halaman sebelumnya
        redirect(base_url().'index.php/home/posting');
    }
}
 
/* End of file home.php */
/* Location: ./application/controllers/home.php */
Perhatikan pada controller diatas terdapat tambahan 2 method yaitu baca dan hapus, dimana masing - masing method memerlukan variabel $id untuk menjalan operasinya
2. Buat model webmodel
Pada contoh kali ini, kami sudah melakukan modifikasi contoh model seperti yang telah kita bahas sebelumnya, silahkan perhatikan contoh script dibawah ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
 
class Webmodel extends CI_Model {
    function __construct()
    {
         
    }
     
    /* Menampilkan posting */
    function list_posting()
    {
        $query = $this->db->query('select * from posting order by PID desc limit 20');
        //lihat apakah ada data dalam tabel
        $num = $query->num_rows();
        if($num>0){
            //Mengirimkan data array hasil query
            return $query->result();
            //Function result() hampir sama dengan function mysql_fetch_array()
        }
        else{
            return 0;
            //Kirimkan 0 jika tidak ada datanya
        }
    }
     
    /* manampilkan posting secara detail */
    function baca_posting($id)
    {
        $query = $this->db->query('select * from posting where PID="'.$id.'"');
        if($query->num_rows()>0){
            $data = $query->result();
        }
        else{
            $data = 0;
        }
        return $data;
    }
     
    function hapus_posting($id)
    {
        $hapus = $this->db->query('delete from posting where PID="'.$id.'"');
    }
}
3. Buat view posting dan baca_posting
Script posting.php akan digunakan untuk menampilkan daftar posting, sedangkan script baca_posting.php akan digunakan untuk menampilkan sebuah posting secara detail
posting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
if(is_array($posting)){
    $data='';
    $no=1;
    foreach($posting as $key){
        $data .= '<tr>
                    <td height="20">'.$no.'</td>
                    <td>'.$key->Judul.'</td>
                    <td>
                        <a href="'.base_url().'index.php/home/baca/'.$key->PID.'">Baca</a> |
                        <a href="'.base_url().'index.php/home/hapus/'.$key->PID.'" onclick="return confirm(\'Wes yakin a?\');">Hapus</a>
                    </td>
                  </tr>';
        $no++;
    }
}
?>
<style type="text/css">
table{
    border:silver 1px solid;
    color:#666666;
}
table tr td{
    padding:0 5px 0 5px;
    border-bottom:silver 1px solid;
    border-right:silver 1px solid;
}
a{
    font:normal 12px Tahoma,Verdana;
    color:blue;
    text-decoration:none;
}
a:hover{
    color:#ff9900;
}
</style>
<table width="670" cellspacing="0" cellpadding="0">
    <tr>
        <td height="25" colspan="3">DAFTAR POSTING TUTORIAL</td>
    </tr>
    <tr>
        <td height="20" width="30">No</td>
        <td width="550">Judul</td>
        <td width="100">Menu</td>
    </tr>
    <?php echo $data; ?>
</table>
Script posting sudah kami lakukan modifikasi untuk menampilkan data dalam bentuk tabel html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<style type="text/css">
    h3.judul{
        display:block;
        line-height:40px;
        border-bottom:silver 2px double;
    }
    p.penulis,p.tanggal{
        display:block;
        line-height:20px;
        border-bottom:silver 2px solid;
    }
</style>
<?php
if(is_array($baca)){
    foreach($baca as $key){
        echo '<h3 class="judul">'. $key->Judul.'</h3>';
        echo $key->Isi;
        echo '<p class="penulis">'. $key->Penulis.'</p>';
        echo '<p class="tanggal">'. $key->PostDate.'</p>';
    }
}
?>
Contoh operasi diatas kami rasa sudah mewakili operasi database lainnya seperti perintah insert dan update. Selamat mencoba dan Salam Kreatif

0 comments:

Post a Comment

 

© Copyright 2010 oleh HariZ| Powered By : Blogger