<?php
class siswa extends CI_Controller{
private $limit=10;
function __construct()
{
parent::__construct();
$this->load->library(array('table','form_validation'));
$this->load->helper(array('form','url'));
$this->load->model('siswa_model','',TRUE);
}
function index($offset=0,$order_column='id',
$order_type='asc')
{
if(empty($offset)) $offset=0;
if(empty($order_column)) $order_column='id';
if(empty($order_type)) $order_type='asc';
$siswas=$this->siswa_model->get_paged_list($this->limit,
$offset,$order_column,$order_type)->result();
$this->load->library('pagination');
$config['base_url'] = site_url('siswa/index/');
$config['total_rows']=$this->siswa_model->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] =3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->library('table');
$this->table->set_empty(" ");
$new_order=($order_type=='asc'?'desc':'asc');
$this->table->set_heading(
'No',
anchor('siswa/index/'.$offset.'/nama/'.$new_order,'Nama'),
anchor('siswa/index/'.$offset.'/alamat/'.$new_order,'Alamat'),
anchor('siswa/index/'.$offset.'/jenis_kelamin/'.$new_order,'Jenis Kelamin'),
anchor('siswa/index/'.$offset.'/tanggal_lahir/'.$new_order,'Tanggal Lahir (dd-mm-yyyy)'),
'Actions'
);
$i=0+$offset;
foreach ($siswas as $siswa) {
$this->table->add_row(++$i,
$siswa->nama,
$siswa->alamat,
strtoupper($siswa->jenis_kelamin) =='M'?
'Laki-Laki':'Perempuan',
date('d-m-Y',strtotime(
$siswa->tanggal_lahir)),
anchor('siswa/view/'.$siswa->id,
'view',array('class'=>'view')).' '.
anchor('siswa/update/'.$siswa->id,
'update',array('class'=>'update')).' '.
anchor('siswa/delete/'.$siswa->id,
'delete',array('class'=>'delete','onclick'=>"return confirm(
'Apakah Anda yakin ingin menghapus data siswa?')"))
);
}
$data['table']=$this->table->generate();
if($this->uri->segment(3)=='delete_success')
$data['message']='Data berhasil dihapus';
else if($this->uri->segment(3)=='add_success')
$data['message']='Data berhasil ditambah';
else
$data['message']='';
$this->load->view('siswaList',$data);
}
function add(){
$data['title'] = 'Tambah siswa baru';
$data['action'] = site_url('siswa/add');
$data['link_back']=anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
$this->_set_rules();
if($this->form_validation->run()==FALSE){
$data['message']='';
$data['title']='Add new siswa';
$data['message']='';
$data['siswa']['id']='';
$data['siswa']['nama']='';
$data['siswa']['alamat']='';
$data['siswa']['jenis_kelamin']='';
$data['siswa']['tanggal_lahir']='';
$data['link_back']=anchor('siswa/index/',
'Lihat Daftar Siswa',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}else{
$siswa=array('nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=>date('Y-m-d',
strtotime($this->input->post('tanggal_lahir'))));
$id=$this->siswa_model->save($siswa);
$this->validation->id = $id;
redirect('siswa/index/add_success');
}
}
function view($id){
$data['title']='siswa Details';
$data['link_back']=anchor('siswa/index/',
'Lihat daftar siswas',array('class'=>'back'));
$data['siswa']=$this->siswa_model->get_by_id($id)->row();
$this->load->view('siswaView',$data);
}
function update($id){
$data['title']='Update siswa';
$this->load->library('form_validation');
$this->_set_rules();
if($this->form_validation->run()=== FALSE){
$data['message']='';
$data['siswa']=$this->siswa_model->get_by_id($id)->row_array();
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
strtoupper($data['siswa']['jenis_kelamin']);
$data['siswa']['tangal_lahir']=date('d-m-Y',
strtotime($data['siswa']['tanggal_lahir']));
$data['title'] = 'Update Siswa';
$data['message']='';
}else{
$id=$this->input->post('id');
$siswa= array('nama' =>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=> date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));
$this->siswa_model->update($id,$siswa);
$data['siswa']=$this->siswa_model->get_by_id($id)->row_array();
$data['message'] ='update siswa success';
}
$data['link_back'] = anchor('siswa/index/','Lihat daftar siswa',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}
function delete($id){
$this->siswa_model->delete($id);
redirect('siswa/index/delete_success','refresh');
}
function _set_rules(){
$this->form_validation->set_rules('nama','Nama','required|trim');
$this->form_validation->set_rules('jenis_kelamin','Password','required');
}
function valid_date($str)
{
if(!preg_match('/^[0-9] {4} -[0-9] {2} - [0-9] {2}$/',$str))
{
$this->form_validation->set_message('valid_date','date format is not valid. yyyy-mm-dd');
return false;
}
else
{
return true;
}
}
}
<?php
Class Model_siswa extends CI_Model
{
private $primary_key ='id';
private $table_name = 'siswa';
function __construct()
{
parent:: __construct();
}
function get_paged_list($limit=10, $offset=0, $order_column ='', $order_type='asc')
{
if (empty($order_column) || empty($order_type))
{
$this->db->order_by($this->primary_key,'asc');
}
else
{
$this->db->order_by($order_column, $order_type);
return $this->db->get($this->table_name,$limit,$offset);
}
}
function count_all()
{
return $this->db->count_all($this->table_name);
}
function get_by_id()
{
$this->db->where($this->primary_key,$id);
return $this->db->get($this->table_name);
}
function save($person)
{
$this->db->insert($this->table_name, $person);
return $this->db->insert_id();
}
function update($id, $person)
{
$this->db->where($this->primary_key, $id);
$this->db->update($this->table_name, $person);
}
function delete($id)
{
$this->db->where($this->primary_key, $id);
$this->db->delete($this->table_name);
}
}
SiswaEdit
<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transilational//EN""http:www.w3.org/TR/xhtml/DTD/xhtml-transinational.dtd">
<htm lxmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type"content="text/html"; charset="iso-8859-1";>
<title>SIMPLE CRUD APPLICATION</title>
<link href="<?php echo base_url(); ?>style/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="content">
<h1><?php echo $title; ?></h1>
<?php echo $message; ?>
<?php echo validation_errors(); ?>
<?php echo form_open($action); ?>
<div class="data">
<table>
<tr>
<td withd ="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo (isset($siswa['id'])) ?$siswa['id']:''; ?>"/></td>
<input type="hidden" name="nama" class="text" value="<?php (isset($siswa['id'])) ?$siswa['id']:''; ?>"/>
</tr>
<tr>
<td valign="top">Nama<span style="color:red";>*<span></span></td>
<td><input type="text" name="nama" class="text" value="<?php echo (set_value($nama)) ?set_value('nama'):$siswa['nama']:''; ?>"/>
<?php echo form_error('alamat'); ?></td>
</tr>
<tr>
<td valign="top">Alamat<span style="color:red";>*<span></span></td>
<td><input type="text" name="alamat" class="text" value="<?php echo (set_value($alamat)) ?set_value('alamat'):$siswa['alamat']:''; ?>"/>
<?php echo form_error('alamat'); ?></td>
</tr>
<tr>
<td valign="top">jenis_kelamin<span style="color:red";>*</span></td>
<td><input type="radio" name="jenis_kelamin" value="M"<?php echo set_radio('jenis_kelamin', 'M', TRUE) ?>>
<?php echo form_error('alamat'); ?></td>
</tr>
<tr>
<td valign="top">Jenis_kelamin<span style="color:red;"></span></td>
<td><input type="radio" name="jenis_kelamin" value="M"<?php echo set_radio('jenis_kelamin','M', TRUE);?> />Laki-laki
<input type="radio" name="jenis_kelamin" value="M"<?php echo set_radio('jenis_kelamin','F', TRUE);?> />Perempuan
<?php echo form_error('jenis_kelamin'); ?></td>
</tr>
<tr>
<td valign="top">Date Of Birth (dd-mm-yyyy) <spanstyle="color:red;">*</spans></td>
<td><input type="text" name="tanggal_lahir" class="text" value="<?php echo (set_value('tanggal_lahir'))? set_value('tanggal_lahir') :$siswa['tanggal_lahir']; ?>"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div>
</form>
<br/>
<?php echo $link_back;?>
</div>
</body>
</html>
SiswaList
<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transilational//EN""http:www.w3.org/TR/xhtml/DTD/xhtml-transinational.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type"content="text/html"; charset="iso-8859-1";>
<title>Simple Crud Application</title>
<link href="<?php echo base_url(); ?>style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="content">
<h1>Contoh Insert Update dan Delete</h1>
<div class="paging"><?php echo $pagination; ?></div>
<div class="data"><?php echo $table; ?></div>
<div class="paging"><?php echo $pagination; ?></div><br/>
<?php echo anchor('siswa/add/','Tambah Siswa baru', array('class'=>'add' ));?>
</div>
</body>
</html>
SiswaView
<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transilational//EN""http:www.w3.org/TR/xhtml/DTD/xhtml-transinational.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type"content="text/html"; charset="iso-8859-1";>
<title>Simple Crud Application</title>
<link href="<?php echo base_url(); ?> style/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>
</tr>
<tr>
<td><?php echo $siswa->id; ?></td>
</tr>
<tr>
<td valign="top">Name</td>
<td><?php echo $siswa->nama ?></td>
</tr>
<tr>
<td valign="top">Alamat</td>
<td><?php echo $siswa->alamat ?></td>
</tr>
<tr>
<td valign="top">Jenis Kelamin</td>
<td><?php echo ($siswa->jenis_kelamin)=='M' ?'Laki-laki':'Perempuan'; ?></td>
</tr>
<tr>
<td valign="top">Tanggal Lahir (dd-mm-yyyy)</td>
<td><?php echo date('d-m-y', strtotime($siswa->tanggal_lahir)) ?></td>
</tr>
</table>
</div>
</div>
</body>
</html>