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

< ?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 »';
	//	$config['prev_link'] 	= '« 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

< ?
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

Guest Book


< ?php $v =& $this->validation ?> < ? if ($v->error_string) { ?>
< ?php echo $v->error_string?>
< ? } ?> < ?php if($success): ?>
Data Sukses Disimpan
< ?php unset($v); endif; ?> < ? if($tguestbook_list) { foreach($tguestbook_list as $value) { echo "
  • ".$value['nama']." ( ".$value['tanggal']." ): ".nl2br(parse_smileys($value['komentar'],"http://localhost/vni/smileys/"))."
    "; } } ?> < ?php echo $page_links;?> < ?echo js_insert_smiley('bukutamu', 'komentar'); ?>

    Isi Buku Tamu

    < ?php echo $smiley_table; ?>
  • Tags: ,

    9 Responses to “Membuat Buku Tamu dengan Codeigniter”

    1. muhidin July 4, 2009 at 6:05 pm #

      mas, lihat script dimana ya?

    2. mardika September 24, 2009 at 4:41 am #

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

    3. Sunyi April 26, 2010 at 9:53 am #

      koq hanya sourcenya aja sich mas ga ada penjelasannya….

      btw… thanks atas sharingnya…

    4. Kamen Rider Kuuga June 26, 2010 at 1:38 pm #

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

    5. me October 6, 2010 at 12:24 pm #

      setengah2 seh mas??

    6. cy January 3, 2011 at 2:41 pm #

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

    7. kholiq January 19, 2011 at 12:50 pm #

      wah tambah mantep aja nih ibnoe… … trims totorialnya…

      salam dari purbalingga…. hehe… :)

    8. zawaruddin March 1, 2011 at 10:46 am #

      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

    9. Mifta December 6, 2012 at 9:14 pm #

      pusing nih kalau PHP begini

    Leave a Reply