Selasa, 20 September 2011

Membuat Buku Tamu dengan Codeigniter



Membuat Buku Tamu dengan Codeigniter
Setelah kita membahas tentang codeigniter sebaiknya kita ambil sebuah project untuk memberikan contoh bagaimana penggunaan codeigniter pada kehidupan nyata. Ini adalah sebuah buku tamu sederhana menggunakan library validasi dan helper smile. anda harus mendowload terlebih dahulu smile-smile yang ada di codeigniter (karena tidak di includekan secara default)

Controller – guestbook.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class Guestbook extends Controller {
 
 function Guestbook()
 {
  parent::Controller();  
         $this->load->library('pagination');
         $this->load->library('validation');
         $this->load->model('tguestbookmodel' , 'guestbook');
 
       }
 
 function show()
 {
 
  if ( $this->_validate_data())
  {
               $data['nama']  = $this->input->post('nama', TRUE);
          $data['email']        = $this->input->post('email', TRUE);
          $data['komentar']  = $this->input->post('komentar', TRUE);
          $data['status']      = 0;
 
 
                    if ($this->guestbook->add($data))
                              $data['success']= 'Stockist sukses ditambahkan';
 
  }
 
               $paging_uri=2;
         if ($this->uri->segment($paging_uri))
   $start=$this->uri->segment($paging_uri);
          else
                        $start=0 ;
 
  $limit_per_page = 10;
  $filter=array('status'=>'1');
 
  $data['tguestbook_list'] = $this->guestbook->findByFilter($filter,$limit_per_page,$start);
 
  $config['base_url']     = site_url('guestbook');
  $config['total_rows']   = $this->guestbook->table_record_count;
  $config['per_page']     = $limit_per_page;
  $config['uri_segment']  = $paging_uri;
 // $config['next_link']  = 'berikutnya &raquo;';
 // $config['prev_link']  = '&laquo; sebelumnya ';
 
  $this->pagination->initialize($config);
 
  $data['page_links'] = $this->pagination->create_links();
 
  $this->load->helper('smiley');
  $this->load->library('table');
 
  $image_array = get_clickable_smileys(base_url().'smileys/');
 
  $col_array = $this->table->make_columns($image_array,20);  
 
  $data['smiley_table'] = $this->table->generate($col_array);
 
  $this->load->view('guestbook', $data);
 }
 
 function _remap($page) 
 {
  $this->show();
        }
 
 function _validate_data()
 {
    $rules['nama'] = "required|htmlspecialchars";
  $rules['email'] = "required|valid_email|htmlspecialchars";
  $rules['komentar'] = "required|htmlspecialchars";
 
 
  $this->validation->set_rules($rules);
 
  $fields['nama'] = 'nama';
  $fields['komentar'] = 'komentar';  
  $fields['email'] = 'email';
 
  $this->validation->set_fields($fields);
 
              return ($this->validation->run() == FALSE) ? FALSE : TRUE;
 }
}

Model – tguestbook_model.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?
class Tguestbookmodel extends Model {
 
 public $table_record_count;
 function Tguestbookmodel()
    {
  parent::Model();
    }
 
  function findAll($fields=NULL,$start = NULL, $count = NULL) 
    {
          return $this->find($fields,NULL, NULL,$start, $count);
    }
 
       function findByFilter($filter_rules, $start = NULL, $count = NULL) 
       {
           return $this->find(NULL,$filter_rules, NULL,$start, $count);
       }
 
       function find($fields=NULL, $filters = NULL, $order=NULL, $start = NULL, $count = NULL) 
       {
                $results = array();
  //finding number of search
  $this->_set_where($filters);
  $this->db->from('guestbook');
               $this->table_record_count = $this->db->count_all_results();
 
  //the real result
  $this->_set_where($filters);
  $order=array("tanggal"=>"desc");
  $this->_set_order($order);
 
 
             if ($start) 
             {
               if ($count) 
                        $this->db->limit($start, $count);
               else 
                       $this->db->limit($start);
              }
            $query = $this->db->get( 'guestbook' );
      if ($query->num_rows() > 0) 
            {
           return $query->result_array();
             }
             else 
              {
   return FALSE;
       }
   }
 
   function add( $data ) 
   {
    $this->db->insert('guestbook', $data);
           return $this->db->insert_id();
   }
 
   function update($keyvalue, $data) 
   {
      $this->db->where('id', $keyvalue);
      $this->db->update('guestbook', $data);
   }
 
   function delete($idField) 
   {
      $this->db->where('id', $idField);
      $this->db->delete('guestbook');
      return true;
   }
 
 function _set_where($filters=NULL)
 {
   if ($filters) 
    {
    if ( is_string($filters) ) 
    {
    $where_clause = $filters;
    $this->db->where($where_clause);
    }
    elseif ( is_array($filters) ) 
    {
    if ( count($filters) > 0 ) 
    {
       foreach ($filters as $field => $value) 
       $this->db->where($field, $value);               
    }
    }
 
    }
 }
 function _set_order($order=NULL)
 {
   if ($order) 
    {
    if ( is_string($order) ) 
    {
    $where_clause = $order;
    $this->db->order_by($where_clause); 
    }
    elseif ( is_array($order) ) 
    {
    if ( count($order) > 0 ) 
    {
       foreach ($order as $field => $value) 
       $this->db->order_by($field, $value);               
    }
    }
 
    }
 }
 
}
 
?>

View – tguestbook_model.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
<h2>Guest Book </h2>
<hr>
<?php $v =& $this->validation ?>
 <? if ($v->error_string) { ?>
  <div class="form_error">
   <?php echo $v->error_string?>
  </div>
<? } ?>
<?php if($success): ?>
 
<div class="success">
 <span class="message_content">Data Sukses Disimpan</span>
</div>
<?php unset($v); endif; ?>
 
<?
if($tguestbook_list)
{
 foreach($tguestbook_list as $value)
 {
  echo "<li><strong><u>".$value['nama']."</u></strong> ( ".$value['tanggal']." ): ".nl2br(parse_smileys($value['komentar'],"http://localhost/vni/smileys/"))." <hr></li>";
 }
}
?>
 
 
<?php echo $page_links;?>
<?echo js_insert_smiley('bukutamu', 'komentar'); ?>
<br>
<h4>Isi Buku Tamu</h4>
<form name="bukutamu" method="post">
<label for="nama" >Nama : </label>
<input type="text" name="nama" value="<?php echo $v->nama?>"/>
<label for="email" >Email : </label>
<input type="text" name="email" value="<?php echo $v->email?>"/>
 
 
<textarea name="komentar" cols="40" rows="4">
<?php echo $v->komentar?>
</textarea>
<div class="no-border">
<?php echo $smiley_table; ?> 
</div>
<input type="Submit" />
</form>

0 comments:

Posting Komentar

http://www.resepkuekeringku.com/2014/11/resep-donat-empuk-ala-dunkin-donut.html http://www.resepkuekeringku.com/2015/03/resep-kue-cubit-coklat-enak-dan-sederhana.html http://www.resepkuekeringku.com/2014/10/resep-donat-kentang-empuk-lembut-dan-enak.html http://www.resepkuekeringku.com/2014/07/resep-es-krim-goreng-coklat-kriuk-mudah-dan-sederhana-dengan-saus-strawberry.html http://www.resepkuekeringku.com/2014/06/resep-kue-es-krim-goreng-enak-dan-mudah.html http://www.resepkuekeringku.com/2014/09/resep-bolu-karamel-panggang-sarang-semut-lembut.html