Berikut Langkah-Langkah Pengerjaan :
1. Menyiapkan Project CodeIgniter
2. Membuat Database
Buat database dan sebuah tabel yang bernama ‘identitas_penduduk’, dengan struktur tabel sebagai berikut :
3. Mengatur Config Database
Edit config pada Database.php menjadi :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'penduduk',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
4. Membuat Controller
Membuat controller dan menambahkan beberapa fungsi seperti berikut :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class penduduk extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('penduduk_models');
}
public function index()
{
$data['identitas_penduduk'] = $this->penduduk_models->getPenduduk();
$this->load->view('list_penduduk', $data);
}
public function insert()
{
$this->load->view('add_penduduk');
}
public function store()
{
$data = array(
'nama_penduduk' => $this->input->post('nama_penduduk'),
'ttl_penduduk' => $this->input->post('ttl_penduduk'),
'jenis_kelamin_penduduk' => $this->input->post('jenis_kelamin_penduduk')
);
$this->penduduk_models->insertPenduduk($data);
$this->index();
}
public function updateView($nik_penduduk)
{
$data['identitas_penduduk'] = $this->penduduk_models->getPendudukSpesific($nik_penduduk);
$this->load->view('edit_penduduk', $data);
}
public function update()
{
$nik_penduduk = $this->input->post('nik_penduduk');
$data = array(
'nama_penduduk' => $this->input->post('nama_penduduk'),
'ttl_penduduk' => $this->input->post('ttl_penduduk'),
'jenis_kelamin_penduduk' => $this->input->post('jenis_kelamin_penduduk')
);
$this->penduduk_models->updatePenduduk($data, $nik_penduduk);
$this->index();
}
public function delete($nik_penduduk)
{
$this->penduduk_models->deletePenduduk($nik_penduduk);
$this->index();
}
}
5. Membuat Model
Membuat model di folder models seperti berikut :
<?php
class penduduk_models extends CI_Model
{
private $db;
// private $_table = 'identitas_penduduk';
public function __construct()
{
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function getPenduduk()
{
$this->db->from('identitas_penduduk');
$query = $this->db->get();
return $query->result();
}
public function getPendudukSpesific($nik_penduduk)
{
$this->db->from('identitas_penduduk');
$this->db->where('nik_penduduk', $nik_penduduk);
$query = $this->db->get();
return $query->result();
}
public function insertPenduduk($data)
{
return $this->db->insert('identitas_penduduk', $data);
}
public function updatePenduduk($data, $nik_penduduk)
{
$this->db->where('nik_penduduk', $nik_penduduk);
return $this->db->update('identitas_penduduk', $data);
}
public function deletePenduduk($nik_penduduk)
{
$this->db->where('nik_penduduk', $nik_penduduk);
return $this->db->delete('identitas_penduduk');
}
}
6. Hasil Pembuatan
View List Penduduk :
Menambah Data Penduduk :
Mengedit Data Penduduk :
Hapus Data Penduduk :
Budiman Akbar Radhiansyah
05111740000179
PBKK A