Anda di halaman 1dari 6

20181 PEMROGRAMAN WEB 2

CRUD DI FRAMEWORK CODEIGNITER-TEMPLATE BOOTSTRAP ADMINLTE (Tabel Satuan)

Pastikan anda sudah menyelesaikan video tutorial yang telah saya publish di channel youtube saya yaitu :
Novinaldi.

Berikut script code yang telah saya buat untuk tabel satuan :

File controller -> Satuan.php


<?php

class Satuan extends CI_Controller {

public function __construct() {


parent::__construct();
$this->load->model('Modelsatuan', 'satuan');
$this->load->library(array('form_validation', 'session'));
}

function index() {
$tombol_cari = $this->input->post('tombolcari', true);
if (isset($tombol_cari)) {
$cari = $this->input->post('cari', true);
$this->session->set_userdata('carisatuan', $cari);
redirect('satuan/index');
} else {
$cari = $this->session->userdata('carisatuan');
}

$querysatuan = $this->satuan->datasatuan($cari);

//Ini Konfigurasi Pagination


$this->load->library('pagination');
$config['base_url'] = site_url('satuan/index/');
$config['total_rows'] = $querysatuan->num_rows();
$config['per_page'] = '5';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['first_link'] = 'Awal';
$config['last_link'] = 'Akhir';
$config['uri_segment'] = 3;

//Custom Pagination
$config['attributes']['rel'] = false;
$config['full_tag_open'] = '<nav aria-label="Page navigation example"><ul
class="pagination">';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item disabled"><li class="page-item active"><a
class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
//custom pagination

$this->pagination->initialize($config);
$uri = $this->uri->segment(3);
$per_page = $config['per_page'];

$querysatuan_perpage = $this->satuan->datasatuan_perpage($cari, $per_page, $uri);

Novinaldi, M.Kom
20181 PEMROGRAMAN WEB 2

$data = array(
'tampil' => $querysatuan_perpage,
'cari' => $cari
);
$template = array(
'judulkonten' => 'Manajemen Data Satuan',
'menusidebar' => $this->load->view('template/menu', '', TRUE),
'konten' => $this->load->view('satuan/viewdata', $data, true),
);
$this->parser->parse('template/halaman', $template);
}

public function tambah() {


$template = array(
'judulkonten' => 'Form Tambah Satuan',
'menusidebar' => $this->load->view('template/menu', '', TRUE),
'konten' => $this->load->view('satuan/formtambah', '', TRUE),
);
$this->parser->parse('template/halaman', $template);
}

public function simpan() {


$nama = $this->input->post('nama', TRUE);

$this->form_validation->set_rules('nama', 'Nama Satuan', 'required', array(


'required' => '%s tidak boleh kosong'
));

if ($this->form_validation->run() == FALSE) {
$session = array(
'pesan' => '<div class="alert alert-danger"><strong>' . validation_errors() .
'</strong></div>'
);
$this->session->set_flashdata($session);
redirect('satuan/tambah');
} else {
$querysimpandata = $this->satuan->simpandata($nama);
if ($querysimpandata) {
$pesansukes = '<div class="alert alert-success"><strong>Data berhasil
disimpan</strong></div>';
$this->session->set_flashdata('pesan', $pesansukes);
redirect('satuan/tambah');
}
}
}

public function edit() {


$kode = $this->uri->segment(3);
$queryambildata = $this->satuan->ambildata($kode);
if ($queryambildata->num_rows() > 0) {
$row = $queryambildata->row_array();
$data = array(
'kode' => $row['satid'],
'nama' => $row['satnama'],
);
} else {
exit('Maaf Data tidak ditemukan');
}
$template = array(
'judulkonten' => 'Form Edit Satuan',
'menusidebar' => $this->load->view('template/menu', '', TRUE),
'konten' => $this->load->view('satuan/formedit', $data, TRUE),
);
$this->parser->parse('template/halaman', $template);
}

public function update() {


$kode = $this->input->post('kode', TRUE);
Novinaldi, M.Kom
20181 PEMROGRAMAN WEB 2

$nama = $this->input->post('nama', TRUE);

$queryupdatedata = $this->satuan->updatedata($kode, $nama);


if ($queryupdatedata) {
$pesansukes = '<div class="alert alert-success"><strong>Data berhasil di-
Update</strong></div>';
$this->session->set_flashdata('pesan', $pesansukes);
redirect('satuan/edit/' . $kode);
}
}

public function hapus() {


$kode = $this->uri->segment(3);
$queryambildata = $this->satuan->ambildata($kode);
if ($queryambildata->num_rows() > 0) {
$this->satuan->hapusdata($kode);
$pesansukes = '<div class="alert alert-success">'
. '<button type="button" class="close" data-dismiss="alert" aria-
hidden="true">×</button>'
. '<strong>Data satuan berhasil di-Hapus</strong>'
. '</div>';
$this->session->set_flashdata('pesan', $pesansukes);
redirect('satuan/index');
} else {
exit('Maaf Data tidak ditemukan');
}
}

File model -> Modelkategori.php


<?php

class Modelsatuan extends CI_Model {

public function datasatuan($cari) {


$arraycari = array(
'satnama' => $cari
);
$this->db->or_like($arraycari);
return $this->db->get('satuan'); //query builder ci
}

public function simpandata($nama) {


$field = array(
'satnama' => $nama
);
return $this->db->insert('satuan', $field);
}

public function ambildata($kode) {


return $this->db->get_where('satuan', array('satid' => $kode));
}

public function updatedata($kode, $nama) {


$field = array(
'satnama' => $nama,
);
$this->db->where('satid', $kode);
return $this->db->update('satuan', $field);
}

public function hapusdata($kode) {


return $this->db->delete('satuan', array('satid' => $kode));
}

Novinaldi, M.Kom
20181 PEMROGRAMAN WEB 2

public function datasatuan_perpage($cari, $per_page, $uri) {


$arraycari = array(
'satnama' => $cari
);
$this->db->or_like($arraycari);
return $this->db->get('satuan', $per_page, $uri);
}

File view -> viewdata.php


<script>
function tambahdata() {
location.href = ('<?php echo site_url('satuan/tambah') ?>');
}
function editdata(k) {
location.href = ('<?php echo site_url('satuan/edit/') ?>' + k);
}
function hapusdata(kode) {
pesan = confirm('Yakin Data Satuan ini dihapus ?');
if (pesan) {
location.href = ('<?php echo site_url('satuan/hapus/') ?>' + kode);
} else
return false;
}
</script>
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">
<button type="button" class="btn btn-primary" onclick="return tambahdata();">
<i class="fa fa-plus"></i> Tambah Data
</button>
</h3>
</div>
<div class="box-body">
<div class="table table-responsive">
<?php echo $this->session->flashdata('pesan') ?>
<?php echo form_open('', array('class' => 'form-horizontal')) ?>
<div class="row form-group">
<div class="col col-md-2">
<label class="form-control-label">Cari Data</label>
</div>
<div class="col-12 col-md-4">
<input type="text" name="cari" value="<?php echo $cari; ?>"
class="form-control" placeholder="Input Nama Satuan">
</div>
<div class="col-12 col-md-2">
<button type="submit" class="btn btn-success" name="tombolcari">
<i class="fa fa-search"></i>
</button>
</div>
</div>
<?php echo form_close(); ?>
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Nama Satuan</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
Novinaldi, M.Kom
20181 PEMROGRAMAN WEB 2

<?php
$nomor = 0 + $this->uri->segment(3);
foreach ($tampil->result_array() as $row):
$nomor++;
?>
<tr>
<td><?php echo $nomor; ?></td>
<td><?php echo $row['satnama']; ?></td>
<td>
<button type="button" class="btn btn-info"
onclick="return editdata('<?php echo
$row['satid'] ?>');" title="Edit Data">
<i class="fa fa-pencil"></i>
</button>
<button type="button" class="btn btn-danger"
onclick="return hapusdata('<?php echo
$row['satid'] ?>');" title="Hapus Data">
<i class="fa fa-remove"></i>
</button>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<div class="pull-right">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
</div>
</div>
</div>
</div>

File view-> formtambah.php


<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">
<button type="button" class="btn btn-warning" onclick="location.href = ('<?php
echo site_url('satuan') ?>')">
<i class="fa fa-backward"></i> Kembali Lihat Data
</button>
</h3>
</div>
<div class="box-body">
<?php echo $this->session->flashdata('pesan'); ?>
<?php echo form_open('satuan/simpan', array('class' => 'form-horizontal')); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Satuan</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="nama" autofocus>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save"></i> Simpan
</button>
</div>
</div>
Novinaldi, M.Kom
20181 PEMROGRAMAN WEB 2

<?php echo form_close(); ?>


</div>
</div>
</div>
</div>

File view-> formedit.php


<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">
<button type="button" class="btn btn-warning" onclick="location.href = ('<?php
echo site_url('satuan') ?>')">
<i class="fa fa-backward"></i> Kembali Lihat Data
</button>
</h3>
</div>
<div class="box-body">
<?php echo $this->session->flashdata('pesan'); ?>
<?php echo form_open('satuan/update', array('class' => 'form-horizontal')); ?>
<input type="hidden" name="kode" value="<?php echo $kode;?>">
<div class="form-group">
<label class="col-sm-2 control-label">Nama Satuan</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="nama" autofocus value="<?php
echo $nama;?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save"></i> Simpan
</button>
</div>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>

Novinaldi, M.Kom