Friday, April 26, 2019

Web 2-11: CI 3


CI 3: CRUD

1.       Buat database: cijuni
2.       Buat tabel: products (5 kolom)
kode
varchar
64
Primary Key
name
varchar
30

harga
integer


gambar
varchar
255

keterangan
text




3.       Buka config/database.php
4.       Copy script ini:
$db['default'] = array(
       'dsn'       => '',
       'hostname' => 'localhost',
       'username' => 'root',
       'password' => '',
       'database' => 'cijuni',
       '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
);

5.       Buka config/autoload.php
6.       Cari $autoload['libraries']
7.       Edit menjadi
$autoload['libraries'] = array('database', 'session');

8.       Buka application/model/
9.       Buat file Product_model.php
10.   Copy ini:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Product_model extends CI_Model
{
    private $_table = "products";

    public $kode;
    public $name;
    public $harga;
    public $gambar= "default.jpg";
    public $keterangan;

    public function rules()
    {
        return [
            ['field' => 'name',
            'label' => 'Name',
            'rules' => 'required'],

            ['field' => 'harga',
            'label' => 'Harga',
            'rules' => 'numeric'],
           
            ['field' => 'keterangan',
            'label' => 'Keterangan',
            'rules' => 'required']
        ];
    }

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

    public function save()
    {
        $post = $this->input->post();
        $this->kode = uniqid();
        $this->name = $post["name"];
        $this->harga= $post["harga"];
        $this->keterangan= $post["keterangan"];
        $this->db->insert($this->_table, $this);
    }

    public function update()
    {
        $post = $this->input->post();
        $this->kode = $post["id"];
        $this->name = $post["name"];
        $this->harga= $post["harga"];
        $this->keterangan= $post["keterangan"];
        $this->db->update($this->_table, $this, array('kode' => $post['id']));
    }

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


11.   Buka application/controllers/admin/
12.   Buat file Products.php
13.   Copy ini:
<?php

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

class Products extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("product_model");
        $this->load->library('form_validation');
    }

    public function index()
    {
        $data["products"] = $this->product_model->getAll();
        $this->load->view("admin/product/list", $data);
    }

    public function add()
    {
        $product = $this->product_model;
        $validation = $this->form_validation;
        $validation->set_rules($product->rules());

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

        $this->load->view("admin/product/new_form");
    }

    public function edit($id = null)
    {
        if (!isset($id)) redirect('admin/products');
      
        $product = $this->product_model;
        $validation = $this->form_validation;
        $validation->set_rules($product->rules());

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

        $data["product"] = $product->getById($id);
        if (!$data["product"]) show_404();
       
        $this->load->view("admin/product/edit_form", $data);
    }

    public function delete($id=null)
    {
        if (!isset($id)) show_404();
       
        if ($this->product_model->delete($id)) {
            redirect(site_url('admin/products'));
        }
    }
}


14.   Buka views/admin
15.   Buat folder product
16.   Buka views/admin/product
17.   Buat file list.php
18.   Copy ini:
<!DOCTYPE html>
<html lang="en">

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

<body id="page-top">

       <?php $this->load->view("admin/_partials/navbar.php") ?>
       <div id="wrapper">

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

                       <div id="content-wrapper">

                                       <div class="container-fluid">

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

                                                       <!-- DataTables -->
                                                       <div class="card mb-3">
                                                                       <div class="card-header">
                                                                                       <a href="<?php echo site_url('admin/products/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>Name</th>
                                                                                                                                                       <th>Harga</th>
                                                                                                                                                       <th>Photo</th>
                                                                                                                                                       <th>Keterangan</th>
                                                                                                                                                       <th>Action</th>
                                                                                                                                       </tr>
                                                                                                                       </thead>
                                                                                                                       <tbody>
                                                                                                                                       <?php foreach ($products as $product): ?>
                                                                                                                                       <tr>
                                                                                                                                                       <td width="150">
                                                                                                                                                                       <?php echo $product->name ?>
                                                                                                                                                       </td>
                                                                                                                                                       <td>
                                                                                                                                                                       <?php echo $product->harga?>
                                                                                                                                                       </td>
                                                                                                                                                       <td>
                                                                                                                                                                       <img src="<?php echo base_url('upload/product/'.$product->gambar) ?>" width="64" />
                                                                                                                                                       </td>
                                                                                                                                                       <td class="small">
                                                                                                                                                                       <?php echo substr($product->keterangan, 0, 120) ?>...</td>
                                                                                                                                                       <td width="250">
                                                                                                                                                                       <a href="<?php echo site_url('admin/products/edit/'.$product->kode) ?>"
                                                                                                                                                                        class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
                                                                                                                                                                       <a onclick="deleteConfirm('<?php echo site_url('admin/products/delete/'.$product->kode) ?>')"
                                                                                                                                                                        href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
                                                                                                                                                       </td>
                                                                                                                                       </tr>
                                                                                                                                       <?php endforeach; ?>

                                                                                                                       </tbody>
                                                                                                       </table>
                                                                                       </div>
                                                                       </div>
                                                       </div>

                                       </div>
                                       <!-- /.container-fluid -->

                                       <!-- Sticky Footer -->
                                       <?php $this->load->view("admin/_partials/footer.php") ?>

                       </div>
                       <!-- /.content-wrapper -->

       </div>
       <!-- /#wrapper -->


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

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

</body>

</html>

19.   Buka view/admin/product/
20.   Buat file new_form.php
21.   Copy ini:
<!DOCTYPE html>
<html lang="en">

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

<body id="page-top">

       <?php $this->load->view("admin/_partials/navbar.php") ?>
       <div id="wrapper">

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

                       <div id="content-wrapper">

                                       <div class="container-fluid">

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

                                                       <?php if ($this->session->flashdata('success')): ?>
                                                       <div class="alert alert-success" role="alert">
                                                                       <?php echo $this->session->flashdata('success'); ?>
                                                       </div>
                                                       <?php endif; ?>

                                                       <div class="card mb-3">
                                                                       <div class="card-header">
                                                                                       <a href="<?php echo site_url('admin/products/') ?>"><i class="fas fa-arrow-left"></i> Back</a>
                                                                       </div>
                                                                       <div class="card-body">

                                                                                       <form action="<?php base_url('admin/product/add') ?>" method="post" enctype="multipart/form-data" >
                                                                                                       <div class="form-group">
                                                                                                                       <label for="name">Name*</label>
                                                                                                                       <input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
                                                                                                                        type="text" name="name" placeholder="Product name" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('name') ?>
                                                                                                                       </div>
                                                                                                       </div>

                                                                                                       <div class="form-group">
                                                                                                                       <label for="harga">Harga*</label>
                                                                                                                       <input class="form-control <?php echo form_error('harga') ? 'is-invalid':'' ?>"
                                                                                                                        type="number" name="harga" min="0" placeholder="Product harga" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('harga') ?>
                                                                                                                       </div>
                                                                                                       </div>


                                                                                                       <div class="form-group">
                                                                                                                       <label for="name">Photo</label>
                                                                                                                       <input class="form-control-file <?php echo form_error('harga') ? 'is-invalid':'' ?>"
                                                                                                                        type="file" name="gambar" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('gambar') ?>
                                                                                                                       </div>
                                                                                                       </div>

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

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

                                                                       </div>

                                                                       <div class="card-footer small text-muted">
                                                                                       * required fields
                                                                       </div>


                                                       </div>
                                                       <!-- /.container-fluid -->

                                                       <!-- Sticky Footer -->
                                                       <?php $this->load->view("admin/_partials/footer.php") ?>

                                       </div>
                                       <!-- /.content-wrapper -->

                       </div>
                       <!-- /#wrapper -->


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

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

</body>

</html>

22.   Buka views/admin/product/
23.   Buat file edit_form.php
24.   Copy ini:
<!DOCTYPE html>
<html lang="en">

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

<body id="page-top">

       <?php $this->load->view("admin/_partials/navbar.php") ?>
       <div id="wrapper">

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

                       <div id="content-wrapper">

                                       <div class="container-fluid">

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

                                                       <?php if ($this->session->flashdata('success')): ?>
                                                       <div class="alert alert-success" role="alert">
                                                                       <?php echo $this->session->flashdata('success'); ?>
                                                       </div>
                                                       <?php endif; ?>

                                                       <!-- Card  -->
                                                       <div class="card mb-3">
                                                                       <div class="card-header">

                                                                                       <a href="<?php echo site_url('admin/products/') ?>"><i class="fas fa-arrow-left"></i>
                                                                                                       Back</a>
                                                                       </div>
                                                                       <div class="card-body">

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

                                                                                                       <input type="hidden" name="id" value="<?php echo $product->kode?>" />

                                                                                                       <div class="form-group">
                                                                                                                       <label for="name">Name*</label>
                                                                                                                       <input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
                                                                                                                        type="text" name="name" placeholder="Product name" value="<?php echo $product->name ?>" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('name') ?>
                                                                                                                       </div>
                                                                                                       </div>

                                                                                                       <div class="form-group">
                                                                                                                       <label for="harga">Harga</label>
                                                                                                                       <input class="form-control <?php echo form_error('harga') ? 'is-invalid':'' ?>"
                                                                                                                        type="number" name="harga" min="0" placeholder="Product harga" value="<?php echo $product->harga?>" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('harga') ?>
                                                                                                                       </div>
                                                                                                       </div>


                                                                                                       <div class="form-group">
                                                                                                                       <label for="name">Photo</label>
                                                                                                                       <input class="form-control-file <?php echo form_error('gambar') ? 'is-invalid':'' ?>"
                                                                                                                        type="file" name="gambar" />
                                                                                                                       <input type="hidden" name="old_image" value="<?php echo $product->gambar?>" />
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('gambar') ?>
                                                                                                                       </div>
                                                                                                       </div>

                                                                                                       <div class="form-group">
                                                                                                                       <label for="name">Keterangan*</label>
                                                                                                                       <textarea class="form-control <?php echo form_error('keterangan') ? 'is-invalid':'' ?>"
                                                                                                                        name="keterangan" placeholder="Product keterangan..."><?php echo $product->keterangan?></textarea>
                                                                                                                       <div class="invalid-feedback">
                                                                                                                                       <?php echo form_error('keterangan') ?>
                                                                                                                       </div>
                                                                                                       </div>

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

                                                                       </div>

                                                                       <div class="card-footer small text-muted">
                                                                                       * required fields
                                                                       </div>


                                                       </div>
                                                       <!-- /.container-fluid -->

                                                       <!-- Sticky Footer -->
                                                       <?php $this->load->view("admin/_partials/footer.php") ?>

                                       </div>
                                       <!-- /.content-wrapper -->

                       </div>
                       <!-- /#wrapper -->

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

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

</body>

</html>



25.   Buka views/admin/product/list.php
26.   Sebelum </body>, copy ini:
<script>
function deleteConfirm(url){
       $('#btn-delete').attr('href', url);
       $('#deleteModal').modal();
}
</script>

27.   Buka views/admin/_partials/modal.php
28.   Di bagian akhir, tambahkan ini
<!-- Logout Delete Confirmation-->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Are you sure?</h5>
        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">Data yang dihapus tidak akan bisa dikembalikan.</div>
      <div class="modal-footer">
        <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
        <a id="btn-delete" class="btn btn-danger" href="#">Delete</a>
      </div>
    </div>
  </div>
</div>


29.   Di browser, buka
http://localhost/ciku/index.php/admin/products/

30.   Selesai.

Sumber: