Membuat CRUD # CRUD CodeIgniter - Part04 Finish - sopian aji

Terbaru

Senin, 05 Oktober 2020

Membuat CRUD # CRUD CodeIgniter - Part04 Finish

1. Membuat Control yakni control Admin.php pada lokasi application\controllers\Admin.php

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

class Admin extends CI_Controller{
  private $view    = "backend/v_admin/";
  private $redirect = "Admin";

  public function __construct()
  {
    parent::__construct();
    $this->load->model('M_admin');
  }

  function index(){
  $read = $this->M_admin->GetAll();
    $data = array(
      'judul'   => "DATA ADMIN",
      'sub'   => "Lihat Admin",
      'read'=> $read
    );
    $this->load->view($this->view.'read', $data);
  }

  public function create(){
    $data = array(
      'judul'   => "DATA ADMIN",
      'sub'   => "Tambah Admin",
      'create'   => ''
    );
    $this->load->view($this->view.'create', $data);
  }

  public function save(){
    $data = array(
         'kd_admin'=> $this->input->post('kd_admin'),
         'nama_admin'=> $this->input->post('nama_admin')
      );
      $this->M_admin->save($data);
      redirect($this->redirect,'refresh');
  }

  public function edit(){
  $kd   = $this->uri->segment(3);
    $data = array(
      'judul'   => "DATA ADMIN",
      'sub'   => "Ubah Admin",
      'edit'   => $this->M_admin->edit($kd)
    );
  $this->load->view($this->view.'edit', $data);
  }

  public function update(){
   $kd = $this->uri->segment(3);
    $data = array(
      'nama_admin'=> $this->input->post('nama_admin')
    );
    $this->M_admin->update($kd,$data);
    redirect($this->redirect,'refresh');
  }

  public function delete(){
  $kd = $this->uri->segment(3);
    $data = array(
      'kd_admin' => $kd
    );
  $this->M_admin->delete($data);
  redirect($this->redirect,'refresh');
  }

}

 

2. membuat Model dengan model M_admin.php pada lokasi application\models\M_admin.php

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

class M_admin extends CI_Model{
  private $table     = 'admin';
  private $pk     = 'kd_admin';

  public function GetAll(){
    $this->db->order_by($this->pk, 'desc');
    return $this->db->get($this->table);
  }

  public function save($data){
    return $this->db->insert($this->table, $data);
  }

  public function edit($kd){
    $this->db->where($this->pk, $kd);
    return $this->db->get($this->table)->row_array();
  }

  public function update($kd,$data){
    $this->db->where($this->pk, $kd);
    return $this->db->update($this->table, $data);
  }

  public function delete($data){
    $this->db->where($data);
    return $this->db->delete($this->table);
  }

}

 

3. membuat view yakni read.php dengan lokasi penyimpanan application\views\backend\v_admin\read.php

<a href="<?php echo site_url('Admin/create') ?>" title="Tombol untuk menambah data">
  <button type="button" name="button">Tambah</button>
</a><br>

<table border="1" width="50%">
  <tr>
    <td>No</td>
    <td>Kode</td>
    <td>Nama</td>
    <td>Aksi</td>
  </tr>
  <?php
  $no=1;
  foreach ($read->result_array() as $row) {
  ?>
  <tr>
    <td><?php echo $no ?></td>
    <td><?php echo $row['kd_admin'] ?></td>
    <td><?php echo $row['nama_admin'] ?></td>
    <td>
      <a href="<?php echo site_url('Admin/edit/'.$row['kd_admin'])?>" title="Tombol untuk ubah data">Ubah</a> |
      <a href="<?php echo site_url('Admin/delete/'.$row['kd_admin'])?>" onclick="javascript: return confirm('Yakin Mau dihapus <?php echo $row['nama_admin'];?>')" title="Tombol untuk hapus data">Hapus</a>
    </td>
  </tr>
  <?php
  $no++;
  }
  ?>
</table>

 

4. membuat view yakni create.php dengan lokasi penyimpanan application\views\backend\v_admin\create.php

<form class="" action="<?php echo site_url ('Admin/save') ?>" method="post">
  <label>Kode Admin</label><br>
  <input type="text" name="kd_admin" value="" placeholder="Masukkan Kode Admin" required oninvalid="this.setCustomValidity('Kode admin Harus Di Isi')" oninput="setCustomValidity('')"><p></p>

  <label>Nama Admin</label><br>
  <input type="text" name="nama_admin" value="" placeholder="Masukkan Nama Admin" required oninvalid="this.setCustomValidity('Nama admin Harus Di Isi')" oninput="setCustomValidity('')"><p></p>

  <button type="submit" name="button">Simpan</button>
  <a href="<?php echo site_url('Admin') ?>"><button type="button" name="button">Batal</button></a>

</form>

 

5. membuat view yakni edit.php dengan lokasi penyimpanan application\views\backend\v_admin\edit.php

<form class="" action="<?php echo site_url('Admin/update/'.$edit['kd_admin']) ?>" method="post">
  <label>Kode Admin</label><br>
  <input type="text" disabled name="kd_admin" value="<?php echo $edit['kd_admin'] ?>">
  <input type="text" hidden name="kd_admin" value="<?php echo $edit['kd_admin'] ?>">
  <p></p>

  <label>Nama Admin</label><br>
  <input type="text" name="nama_admin" value="<?php echo $edit['nama_admin'] ?>"><p></p>

  <button type="submit" name="button">Perbaharui</button>
  <a href="<?php echo site_url('Admin') ?>"><button type="button" name="button">Batal</button></a>

</form>

Tidak ada komentar:

Posting Komentar