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 />";
	}
}
?>
 
 
< ?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>

 

8 Comments

 

  1. July 4, 2009  6:05 pm by muhidin Reply

    mas, lihat script dimana ya?

  2. September 24, 2009  4:41 am by mardika Reply

    mas isi dan script ditaruh dimana sih, koq ka ga ada?

  3. April 26, 2010  9:53 am by Sunyi Reply

    koq hanya sourcenya aja sich mas ga ada penjelasannya....

    btw... thanks atas sharingnya...

  4. June 26, 2010  1:38 pm by Kamen Rider Kuuga Reply

    Mas Bisa Di Download Gak source codenya.... tambah dowload sourcer nya donk... untuk dipahami lebih jelas...

  5. October 6, 2010  12:24 pm by me Reply

    setengah2 seh mas??

  6. January 3, 2011  2:41 pm by cy Reply

    database nya aja g d kasih tau....
    apalagi link downloadnya

  7. January 19, 2011  12:50 pm by kholiq Reply

    wah tambah mantep aja nih ibnoe... ... trims totorialnya...


    salam dari purbalingga.... hehe... :)

  8. March 1, 2011  10:46 am by zawaruddin Reply

    bro... minta tutor jQuery + CI buat validasi dunk....
    klo bisa validasinya pake jQuery n sent data post (pas validasi sukses) pake CI dunk..
    need help..

    thx b4

Leave a reply

 

Your email address will not be published.