hallo semua kali ini saya mo share... contoh aplikasi crud menggunakan codeigniter...
ok... mari kita belajar bersama.. karena saya juga baru dalam dunia CI... ini hanya sebagai dokumentasi saja....
fitur yang ada pada aplikasi sederhana ini yakni..:
- Edit, Hapus, View
- Input Data
- pagination
Mari kita mulai...
untuk setinggan CI saya anggap semua sudah bisa... klo blom silakan cari di google...
Membuat file controller : person.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Person extends CI_Controller {
// num of records per page
private $limit = 10;
function __construct()
{
parent::__construct();
// load library
$this->load->library(array('table','form_validation'));
// load helper
$this->load->helper('url');
// load model
$this->load->model('Person_model','',TRUE);
}
function index($offset = 0)
{
// offset
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
// load data
$persons = $this->Person_model->get_paged_list($this->limit, $offset)->result();
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('person/index/');
$config['total_rows'] = $this->Person_model->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// generate table data
$this->load->library('table');
$this->table->set_empty(" ");
$this->table->set_heading('No', 'Nama', 'Jenis Kelamin', 'Tanggal Lahir (dd-mm-yyyy)', 'Actions');
$i = 0 + $offset;
foreach ($persons as $person)
{
$this->table->add_row(++$i, $person->name, strtoupper($person->gender)=='M'? 'Pria':'Perempuan', date('d-m-Y',strtotime($person->dob)),
anchor('person/view/'.$person->id,'view',array('class'=>'view')).' '.
anchor('person/update/'.$person->id,'update',array('class'=>'update')).' '.
anchor('person/delete/'.$person->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')"))
);
}
$data['table'] = $this->table->generate();
// load view
$this->load->view('personList', $data);
}
function add()
{
// set empty default form field values
$this->_set_fields();
// set validation properties
$this->_set_rules();
// set common properties
$data['title'] = 'Add new person';
$data['message'] = '';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// load view
$this->load->view('personEdit', $data);
}
function addPerson()
{
// set common properties
$data['title'] = 'Add new person';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// set empty default form field values
$this->_set_fields();
// set validation properties
$this->_set_rules();
// run validation
if ($this->form_validation->run() == FALSE)
{
$data['message'] = '';
}
else
{
// save data
$person = array('name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
$id = $this->Person_model->save($person);
// set user message
$data['message'] = '<div class="success">add new person success</div>';
}
// load view
$this->load->view('personEdit', $data);
}
function view($id)
{
// set common properties
$data['title'] = 'Person Details';
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// get person details
$data['person'] = $this->Person_model->get_by_id($id)->row();
// load view
$this->load->view('personView', $data);
}
function update($id)
{
// set validation properties
$this->_set_rules();
// prefill form values
$person = $this->Person_model->get_by_id($id)->row();
$this->form_data->id = $id;
$this->form_data->name = $person->name;
$this->form_data->gender = strtoupper($person->gender);
$this->form_data->dob = date('d-m-Y',strtotime($person->dob));
// set common properties
$data['title'] = 'Update person';
$data['message'] = '';
$data['action'] = site_url('person/updatePerson');
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// load view
$this->load->view('personEdit', $data);
}
function updatePerson()
{
// set common properties
$data['title'] = 'Update person';
$data['action'] = site_url('person/updatePerson');
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// set empty default form field values
$this->_set_fields();
// set validation properties
$this->_set_rules();
// run validation
if ($this->form_validation->run() == FALSE)
{
$data['message'] = '';
}
else
{
// save data
$id = $this->input->post('id');
$person = array('name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
$this->Person_model->update($id,$person);
// set user message
$data['message'] = '<div class="success">update person success</div>';
}
// load view
$this->load->view('personEdit', $data);
}
function delete($id)
{
// delete person
$this->Person_model->delete($id);
// redirect to person list page
redirect('person/index/','refresh');
}
// set empty default form field values
function _set_fields()
{
$this->form_data->id = '';
$this->form_data->name = '';
$this->form_data->gender = '';
$this->form_data->dob = '';
}
// validation rules
function _set_rules()
{
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
$this->form_validation->set_rules('dob', 'DoB', 'trim|required|callback_valid_date');
$this->form_validation->set_message('required', '* required');
$this->form_validation->set_message('isset', '* required');
$this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
}
// date_validation callback
function valid_date($str)
{
//match the format of the date
if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[1],$parts[3]))
return true;
else
return false;
}
else
return false;
}
}
?>
Membuat 3 File view : personView.php, personList dan personEdit
1. PersonView.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CONTOH APLIKASI CRUD</title>
<link href="<?php echo base_url(); ?>res/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="content">
<h1><?php echo $title; ?></h1>
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><?php echo $person->id; ?></td>
</tr>
<tr>
<td valign="top">Name</td>
<td><?php echo $person->name; ?></td>
</tr>
<tr>
<td valign="top">Gender</td>
<td><?php echo strtoupper($person->gender)=='M'? 'Male':'Female' ; ?></td>
</tr>
<tr>
<td valign="top">Date of birth (dd-mm-yyyy)</td>
<td><?php echo date('d-m-Y',strtotime($person->dob)); ?></td>
</tr>
</table>
</div>
<br />
<?php echo $link_back; ?>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CONTOH APLIKASI CRUD</title>
<link href="<?php echo base_url(); ?>res/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="content">
<h1>CONTOH APLIKASI CRUD</h1>
<div class="paging"><?php echo $pagination; ?></div>
<div class="data"><?php echo $table; ?></div>
<br />
<?php echo anchor('person/add/','add new data',array('class'=>'add')); ?>
</div>
</body>
</html>
3. personEdit.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CONTOH APLIKASI CRUD</title>
<link href="<?php echo base_url(); ?>res/css/style.css" rel="stylesheet" type="text/css" />
<link href="<?php echo base_url(); ?>res/css/calendar.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo base_url(); ?>res/js/calendar.js"></script>
</head>
<body>
<div class="content">
<h1><?php echo $title; ?></h1>
<?php echo $message; ?>
<form method="post" action="<?php echo $action; ?>">
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo set_value('id'); ?>"/></td>
<input type="hidden" name="id" value="<?php echo set_value('id',$this->form_data->id); ?>"/>
</tr>
<tr>
<td valign="top">Name<span style="color:red;">*</span></td>
<td><input type="text" name="name" class="text" value="<?php echo set_value('name',$this->form_data->name); ?>"/>
<?php echo form_error('name'); ?>
</td>
</tr>
<tr>
<td valign="top">Gender<span style="color:red;">*</span></td>
<td><input type="radio" name="gender" value="M" <?php echo set_radio('gender', 'M', $this->form_data->gender == 'M'); ?>/> M
<input type="radio" name="gender" value="F" <?php echo set_radio('gender', 'F', $this->form_data->gender == 'F'); ?>/> F
<?php echo form_error('gender'); ?>
</td>
</tr>
<tr>
<td valign="top">Date of birth (dd-mm-yyyy)<span style="color:red;">*</span></td>
<td><input type="text" name="dob" onclick="displayDatePicker('dob');" class="text" value="<?php echo set_value('dob',$this->form_data->dob); ?>"/>
<a href="javascript:void(0);" onclick="displayDatePicker('dob');"><img src="<?php echo base_url(); ?>res/css/images/calendar.png" alt="calendar" border="0"></a>
<?php echo form_error('dob'); ?></td>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div>
</form>
<br />
<?php echo $link_back; ?>
</div>
</body>
</html>
Yang Terakhir kita membuat file Model : person_model.php
<?php
class Person_model extends CI_Model {
private $tbl_person= 'tbl_person';
function __construct(){
parent::__construct();
}
function list_all(){
$this->db->order_by('id','asc');
return $this->db->get($tbl_person);
}
function count_all(){
return $this->db->count_all($this->tbl_person);
}
function get_paged_list($limit = 10, $offset = 0){
$this->db->order_by('id','asc');
return $this->db->get($this->tbl_person, $limit, $offset);
}
function get_by_id($id){
$this->db->where('id', $id);
return $this->db->get($this->tbl_person);
}
function save($person){
$this->db->insert($this->tbl_person, $person);
return $this->db->insert_id();
}
function update($id, $person){
$this->db->where('id', $id);
$this->db->update($this->tbl_person, $person);
}
function delete($id){
$this->db->where('id', $id);
$this->db->delete($this->tbl_person);
}
}
?>
Selesai... selamat mencoba.... !!
download soarce... Download Disini
hasilnya seperti ini...
Responses
8 Respones to "CONTOH APLIKASI CRUD (Codeigniter)"
nice share, tapi blh minta dump file databasenya gak? bingung nih tabel sama fieldnya apa saja.
makasih..
share via email aja ke: wenaslmv@live.com
August 27, 2012 at 3:33 AM
download aja bang....
udah ane sediain kok....
November 29, 2012 at 1:00 AM
mantap sekali abang.....tapi mau nanya dikit ni..! gimana caranya query menampilkan data yang baru kita input ke database dan tapilkan data lebih awal...jangan di akhir...
July 18, 2013 at 7:03 AM
tinggal ganti aja desc jadi ascending...
July 19, 2013 at 7:59 AM
Databasenya mana gan.; yang seperti itu
February 19, 2016 at 5:42 PM
database nya mana gan
March 23, 2016 at 12:59 AM
table nya apa gan?
May 5, 2016 at 6:42 PM
error
November 9, 2016 at 7:53 PM
Post a Comment