$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'tokobuahfatya',
'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
);
Perhatikan bagian username, password dan databasenya. Untuk username, jika anda
tidak menggunakan username dan password untuk masuk ke databasenya, maka isikan
saja username dengan root dan kosongkan passwordnya. Lalu untuk database,
sesuaikan dengan nama database yang anda buat.
Lalu buka buat file autoload.php dan masukkan codingan seperti ini:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$autoload['packages'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in system/libraries/ or your
| application/libraries/ directory, with the addition of the
| 'database' library, which is somewhat of a special case.
|
| Prototype:
|
| $autoload['libraries'] = array('database', 'email', 'session');
|
| You can also supply an alternative library name to be assigned
| in the controller:
|
| $autoload['libraries'] = array('user_agent' => 'ua');
*/
$autoload['libraries'] = array('database', 'session');
$autoload['drivers'] = array();
$autoload['helper'] = array('url');
$autoload['config'] = array();
$autoload['language'] = array();
$autoload['model'] = array();
public $product_id;
public $name;
public $price;
public $image = "default.jpg";
public $description;
Perhatikan pada bagian class Product_model, $_table harus sama dengan nama tabel di
database, dan isiannya harus sama dengan nama kolom yang ada ditabelnya.
4. Membuat Controller
Buka folder admin yang ada didalam folder controller, kemudian buat file dengan nama
Products.php. Lalu isikan perintahnya:
<?php
if ($validation->run()) {
$product->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/product/new_form");
}
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);
}
if ($this->product_model->delete($id)) {
redirect(site_url('admin/products'));
}
}
}
5. Membuat Tampilan
Disini kita akan membuat 3 tampilan, yaitu tampilan untuk menampilkan data, tampilan
untuk menambah data dan tampilan untuk mengedit data, yang akan disimpan dalam
folder bernama product didalam folder admin yang ada dalam folder views.
a. Tampilan untuk menampilkan data
Buat file bernama list.php, lalu begini codingnya:
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load-
>view("admin/_partials/breadcrumb.php") ?>
<div class="table-responsive">
<table class="table
table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Photo</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td width="150">
</td>
<td>
</td>
<td>
</td>
<td class="small">
<td width="250">
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<script>
function deleteConfirm(url){
$('#btn-delete').attr('href', url);
$('#deleteModal').modal();
}
</script>
</body>
</html>
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load-
>view("admin/_partials/breadcrumb.php") ?>
<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">
<div class="form-
group">
<label
for="price">Price*</label>
<input
class="form-control <?php echo form_error('price') ? 'is-invalid':'' ?>"
<div class="form-
group">
<label
for="name">Photo</label>
<input
class="form-control-file <?php echo form_error('price') ? 'is-invalid':'' ?>"
type="file"
name="image" />
<div
class="invalid-feedback">
<div class="form-
group">
<label
for="name">Description*</label>
<textarea
class="form-control <?php echo form_error('description') ? 'is-invalid':'' ?>"
</div>
</div>
<!-- /.container-fluid -->
<!-- Sticky Footer -->
<?php $this->load->view("admin/_partials/footer.php")
?>
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
</body>
</html>
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load-
>view("admin/_partials/breadcrumb.php") ?>
<form action="<?php
base_url('admin/product/edit') ?>" method="post" enctype="multipart/form-data">
<input type="hidden"
name="id" value="<?php echo $product->product_id?>" />
<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">
<div class="form-
group">
<label
for="price">Price</label>
<input
class="form-control <?php echo form_error('price') ? 'is-invalid':'' ?>"
<div class="form-
group">
<label
for="name">Photo</label>
<input
class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file"
name="image" />
<input
type="hidden" name="old_image" value="<?php echo $product->image ?>" />
<div
class="invalid-feedback">
<div class="form-
group">
<label
for="name">Description*</label>
<textarea
class="form-control <?php echo form_error('description') ? 'is-invalid':'' ?>"
</div>
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
</body>
</html>
Lalu, buat file modal.php di folder _partials, dan masukkan coding berikut:
<!-- 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>
7. Jangan lupa save pekerjaan anda, lalu coba jalankan
localhost/fruitshopfatya/index.php/admin/products
Tampilannya akan seperti ini:
Tambah Data
Tampil Data
Hapus Data
Edit Data
8. Fitur Upload Gambar
a. Buat folder baru didalam projek bernama upload, kemudian buat folder bernama
product.
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->description = $post["description"];
$this->db->update($this->_table, $this, array('product_id' => $post['id']));
f. Jangan lupa simpan, kemudian coba tambahkan field lagi dan jangan lupa upload
gambarnya.
Tampilannya akan seperti ini: