Anda di halaman 1dari 6

CONTROLLER SISWA

<?php
// berfungsi agar tidak terjadi akses langsung ke file ini
defined('BASEPATH') OR exit('No direct script access allowed');
class Siswa extends CI_Controller
// Method __construct() merupakan sebuah konstruktor. Method ini yang akan dieksekusi pertama kali saat
Controller diakses
{
public function __construct()
// Library form_validation akan kita gunakan untuk memvalidasi input pada method add() dan edit()
{
parent::__construct();
$this->load->model("siswa_model");
$this->load->library('form_validation');
}

public function index()


{
$data["siswa"] = $this->siswa_model->getAll();
$this->load->view("admin/siswa/list_siswa", $data);
}

public function add()


{
$siswa = $this->siswa_model; // objek model
$validation = $this->form_validation; // objek form validasi
$validation->set_rules($siswa->rules()); // menerapkan rules

// melakukan validasi, jika valid akan disimpan dan menampilkan pesan "Berhasil disimpan”
if ($validation->run()) {
$siswa->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
// menampilkan data yang telah disimpan ke view
$this->load->view("admin/siswa/input_siswa");
}

public function edit($id = null)


{
if (!isset($id)) redirect('admin/siswa'); // akan di-redirect jika data $id tidak ditemukan

$siswa = $this->siswa_model;
$validation = $this->form_validation;
$validation->set_rules($siswa->rules());

if ($validation->run()) {
$siswa->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}

$data["siswa"] = $siswa->getById($id); // mengambil data untuk ditampilkan di form edit


if (!$data["siswa"]) show_404(); // jika data tidak ditemukan, akan tampil error 404

1
$this->load->view("admin/siswa/edit_siswa", $data); // menampilkan form edit
}

public function delete($id=null)


{
if (!isset($id)) show_404();

if ($this->siswa_model->delete($id)) { // jika data berhasil dihapus, maka kita langsung alihkan


redirect(site_url('admin/siswa')); // (redirect()) menuju ke halaman admin/products/.
}
}
}

2
MODEL SISWA
<?php defined('BASEPATH') OR exit('No direct script access allowed');

//deklarasi pembuatan class model


class Siswa_model extends CI_Model
{
private $_table = "siswa";

public $id_siswa;
public $namasiswa;
public $jk;
public $alamat;

// fungsi ini untuk memberikan aturan validasi input


public function rules()
{
return [
['field' => 'namasiswa',
'label' => 'Nama Siswa',
'rules' => 'required'],

['field' => 'jk',


'label' => 'Jenis Kelamin',
'rules' => 'required'],

['field' => 'alamat',


'label' => 'Alamat',
'rules' => 'required']
];
}

// fungsi untuk mengambil semua data hasil query


public function getAll()
{
return $this->db->get($this->_table)->result();
}

// fungsi untuk mengambil satu data hasil query


public function getById($id)
{
return $this->db->get_where($this->_table, ["id_siswa" => $id])->row();
}

// fungsi untuk menyimpan input


public function save()
{
$post = $this->input->post(); // memasukkan data dari input query ke database
$this->id_siswa = uniqid(); // membuat id unik pada database, sudah kita buat primary key tabel
$this->namasiswa = $post["namasiswa"]; // isi field name ke database
$this->jk = $post["jk"]; // isi field price ke database
$this->alamat = $post["alamat"]; // isi field description ke database
$this->db->insert($this->_table, $this); // menyimpan data ke database

3
}

// fungsi untuk update input


public function update()
{
$post = $this->input->post();
$this->id_siswa = $post["id"];
$this->namasiswa = $post["namasiswa"];
$this->jk = $post["jk"];
$this->alamat = $post["alamat"];
$this->db->update($this->_table, $this, array('id_siswa' => $post['id'])); // update database
}

// fungsi untuk menghapus input


public function delete($id)
{
return $this->db->delete($this->_table, array("id_siswa" => $id));
}
}

4
VIEW INPUT SISWA
<!DOCTYPE html>
<html lang="en">

<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>

<body id="page-top">
====================================================================================================================
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/siswa/') ?>"><i class="fas fa-arrow-left"></i> Back</a>
</div>
<!-- --------------------------------- START CARD BODY -------------------------------------
<div class="card-body">

<form action="<?php base_url('admin/siswa/add') ?>" method="post" enctype="multipart/form-data" >


<div class="form-group">
<label for="namasiswa">Nama*</label>
<input class="form-control <?php echo form_error('namasiswa') ? 'is-invalid':'' ?>"
type="text" name="namasiswa" placeholder="Nama Siswa" />
<div class="invalid-feedback">
<?php echo form_error('namasiswa') ?>
</div>
</div>

<div class="form-group">
<label for="jk">Jenis Kelamin*</label>
<input class="form-control <?php echo form_error('jk') ? 'is-invalid':'' ?>"
type="text" name="jk" placeholder="Jenis Kelamin" />
<div class="invalid-feedback">
<?php echo form_error('jk') ?>
</div>
</div>

<div class="form-group">
<label for="alamat">Alamat*</label>
<textarea class="form-control <?php echo form_error('alamat') ? 'is-invalid':'' ?>"
name="alamat" placeholder="Alamat siswa..."></textarea>
<div class="invalid-feedback">
<?php echo form_error('alamat') ?>
</div>
</div>

<input class="btn btn-success" type="submit" name="btn" value="Save" />


</form>
</div>
<!-- --------------------------------- END CARD BODY -------------------------------------

=================================== FOOTER

</body>

</html>

5
VIEW LIST SISWA
<!DOCTYPE html>
<html lang="en">

<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>

<body id="page-top">
=================================================================
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/siswa/add') ?>"><i class="fas fa-plus"></i> Add New</a>
</div>

<div class="card-body">

<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Nama Siswa</th>
<th>Jenis Kelamin</th>
<th>Alamat</th>
<th>Aksi</th>
</tr>
</thead>

<tbody>
<?php foreach ($siswa as $siswa): ?>
<tr>
<td width="150">
<?php echo $siswa->namasiswa ?>
</td>

<td>
<?php echo $siswa->jk ?>
</td>

<td class="small">
<?php echo substr($siswa->alamat, 0, 120) ?>...</td>

<td width="250">
<a href="<?php echo site_url('admin/siswa/edit/'.$siswa->id_siswa) ?>"
class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
<a onclick="deleteConfirm('<?php echo site_url('admin/siswa/delete/'.$siswa->id_siswa)
?>')"
href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
</td>
</tr>

<?php endforeach; ?>

</tbody>
</table>
</div>
</div>
</div>
<!-- --------------END DIV CLASS CARD MB-3 ----------------- 

</div>
<!-- --------------END DIV CLASS CONTAINER ----------------- 