Skip to main content

add/edit/delete

August 5, 2009 by cah_ajus

cah_ajus's picture

hehehe....aLLow smua..,'saia newbi CI..,
ada contoh script ngk ttg add/edit/delete...,???ble minta donx..,'please help me yach para master.,'

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Contoh sederhana

August 10, 2009 by eien, 1 year 17 weeks ago
Comment: 5614

eien's picture

pertama-tama kita definisikan dulu tabelnya di MYSQL

CREATE TABLE `blogs` (
`blog_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`blog_title` VARCHAR( 255 ) NOT NULL ,
`blog_content` TEXT NOT NULL
) ENGINE = MYISAM ;

setelah mendefinisikan tabelnya, langkah selanjutnya adalah membuat modelnya, bagian ini optional sebenernya, tapi akan lebih baik klo dibuat modelnya.

system/application/models/blog_model.php

class Blog_model extends Model {
 
    function Blog_model(){
        parent::Model();
    }
	function get_all_blog() {
		$this->load->database();
		$query = $this->db->get('blogs');
    	return $query;
  	}
	function get_blog($id) {
		$this->load->database();
		$this->db->where('blog_id',$id);
		$query = $this->db->get('blogs');
    	return $query;
  	}
	function delete_blog($id) {
		$this->load->database();
		$this->db->delete('blogs', array('blog_id' => $id)); 
	}
}

selanjutnya controller

system/application/controllers/blog.php

class Blog extends Controller {
 
	function Blog(){
        parent::Controller();
    }
	function index(){
		$this->load->database();
		$this->load->model('blog_model');
 
		$data['results'] = $this->blog_model->get_all_blog();
 
		$this->load->view('showblog', $data);
	}
	function delete($id){
		$this->load->model('blog_model');
 
		$this->blog_model->delete_blog($id);
 
		redirect('blog','location'); //redirect setelahnya
	}
	function add(){
		$this->load->database();
		$this->load->library('form_validation');
 
		$this->form_validation->set_rules('title', 'Blog Title', 'required');
		$this->form_validation->set_rules('content', 'Blog Content', 'required');
 
		if ($this->form_validation->run() == FALSE ){
			$this->load->view('addblog');
		}
		else{	
			$data = array( 
				'blog_title' => $this->input->post('title'),
				'blog_content' => $this->input->post('content')
			);
			$this->db->insert('blogs', $data);  
 
			redirect('blog','location'); //redirect setelahnya
		}
	}
	function edit(){
		$id=$this->uri->segment(3);
		$this->load->database();
		$this->load->model('blog_model');
		$this->load->library('form_validation');
 
		$this->form_validation->set_rules('title', 'Blog Title', 'required');
		$this->form_validation->set_rules('content', 'Blog Content', 'required');
 
		if ($this->form_validation->run() == FALSE ){
			if(empty($id)) $id=$this->input->post('id');
			$data['result']=$this->blog_model->get_blog($id);		
			$data['id']=$id;
			$this->load->view('editblog', $data);
		}
		else{	
			$id=$this->input->post('id');
 
			$data = array( 
				'blog_title' => $this->input->post('title'),
				'blog_content' => $this->input->post('content')
			);
			$this->db->where('blog_id', $id);
			$this->db->update('blogs', $data); 
 
			redirect('blog','location'); //redirect setelahnya
		}
	}
}

berikutnya di view
system/application/views/showblog.php

                if ($results->num_rows() > 0){
                   foreach ($results->result() as $row){  
                      echo 'Judul : $row->blog_title'.
							anchor('blog/edit/'.$row->blog_id,'edit').
							anchor('blog/delete/'.$row->blog_id,'delete');
                   }
                }else {
                    echo 'Empty Blog';
                }
 

system/application/views/addblog.php

			echo form_open_multipart('blog/add', '').
				  form_label('Title', 'title').
				  form_input('title',set_value('title')).form_error('title').'<br>'.
				  form_label('Content', 'content').'<br>'.
				  form_textarea('content',set_value('content')).form_error('content').'<br>'.
				  form_submit('submit', 'Submit');
 

system/application/views/addblog.php

			$fetch=$result->row();
			echo form_open_multipart('blog/edit', '').
				  form_hidden('id',$id).
				  form_input('title',$fetch->blog_title).form_error('title').'<br>'.
				  form_label('Content', 'content').'<br>'.
				  form_textarea('content',$fetch->blog_content).form_error('content').'<br>'.
				  form_submit('submit', 'Submit');
 

Premium Drupal Themes by Adaptivethemes