Langsung ke konten utama

FP PBKK

MEMBUAT OLSHOP DENGAN CRUD CODEIGNITER

MSP Bimasakti 05111840000070
David Bintang 05111840000090


PEMBUATAN HALAMAN UTAMA

1. Membuat database toko.sql kemudian membuat table products di MySQL.
CREATE TABLE `products` (
`id` int(10) NOT NULL,
`name` varchar(50) NOT NULL,
`description` text NOT NULL,
`price` int(9) NOT NULL,
`stock` int(3) NOT NULL,
`image` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `products`
ADD PRIMARY KEY (`id`);

Struktur dari table products:

2. Membuat file Welcome.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('model_products');
}
public function index()
{
$data['products'] = $this->model_products->all();
$this->load->view('welcome_message', $data);
}
public function add_to_cart($product_id)
{
$product = $this->model_products->find($product_id);
$data = array(
'id' => $product->id,
'qty' => 1,
'price' => $product->price,
'name' => $product->name
);
$this->cart->insert($data);
redirect(base_url());
}
public function cart(){
$this->load->view('show_cart');
}
public function clear_cart()
{
$this->cart->destroy();
redirect(base_url());
}
}
/* Yunan Helmi Al Anbarry */
/* Toko Online*/
3. Membuat file Model_products di dalam folder C:\xampp\htdocs\toko-online\application\models.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_products extends CI_Model {
public function all(){
$hasil = $this->db->get('products');
if($hasil->num_rows() > 0){
return $hasil->result();
} else {
return array();
}
}
public function find($id){
//Query mencari record berdasarkan ID-nya
$hasil = $this->db->where('id', $id)
->limit(1)
->get('products');
if($hasil->num_rows() > 0){
return $hasil->row();
} else {
return array();
}
}
}
4. Membuat file top_layout.php di dalam folder C:\xampp\htdocs\toko-online\application\views\layout.
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?=anchor(base_url(), 'Toko Online', ['class'=>'navbar-brand'])?>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><?php echo anchor(base_url(), 'Home');?></li>
<li>
<?php
$text_cart_url = '<span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span>';
$text_cart_url .= ' Shopping Cart: '. $this->cart->total_items() .' items';
?>
<?=anchor('welcome/cart', $text_cart_url)?>
</li>
<?php if($this->session->userdata('username')) { ?>
<li><div style="line-height:50px;">You Are : <?=$this->session->userdata('username')?></div></li>
<li><?php echo anchor('logout', 'Logout');?></li>
<?php } else { ?>
<li><?php echo anchor('login', 'Login');?></li>
<?php } ?>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
5. Membuat file welcome_message.php di dalam folder C:\xampp\htdocs\toko-online\application\views.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toko Online Inwepo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<?php $this->load->view('layout/top_menu') ?>
<!-- Tampilkan semua produk -->
<div class="row">
<!-- looping products -->
<?php foreach($products as $product) : ?>
<div class="col-sm-3 col-md-3">
<div class="thumbnail">
<?=img([
'src' => 'uploads/' . $product->image,
'style' => 'max-width: 100%; max-height:100%; height:100px'
])?>
<div class="caption">
<h3 style="min-height:60px;"><?=$product->name?></h3>
<p><?=$product->description?></p>
<p>Rp. <?=$product->price?></p>
<p>
<?=anchor('welcome/add_to_cart/' . $product->id, 'Buy' , [
'class' => 'btn btn-primary',
'role' => 'button'
])?>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
<!-- end looping -->
</div>
</body>
</html>
Berikut ini tampilan dari welcome_message atau halaman utama yang menampilkan produk dari toko online.

6. Membuat file show_cart.php di dalam folder C:\xampp\htdocs\toko-online\application\views.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toko Online | Inwepo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<?php $this->load->view('layout/top_menu') ?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Product</th>
<th>Jumlah</th>
<th>Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach ($this->cart->contents() as $items) :
$i++;
?>
<tr>
<td><?= $i ?></td>
<td><?= $items['name'] ?></td>
<td><?= $items['qty'] ?></td>
<td align="right"><?= number_format($items['price'],0,',','.') ?></td>
<td align="right"><?= number_format($items['subtotal'],0,',','.') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td align="right" colspan="4">Total </td>
<td align="right"><?= number_format($this->cart->total(),0,',','.'); ?></td>
</tr>
</tfoot>
</table>
<div align="center">
<?= anchor('welcome/clear_cart','Clear Cart',['class'=>'btn btn-danger']) ?>
<?= anchor(base_url(),'Continue Shopping',['class'=>'btn btn-primary']) ?>
<?= anchor('order','Check Out',['class'=>'btn btn-success']) ?>
</div>
</body>
</html>
Berikut ini tampilan dari show_cart yang berguna untuk menampilkan daftar belanja dari customer.

PEMBUATAN REGISTER DAN LOGIN

1. Membuat table users di dalam database toko.
CREATE TABLE `users` (
`id` int(10) NOT NULL,
`username` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`group` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);

 2.  Membuat file Regis.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Regis extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('model_regis');
}
public function index(){
$username=$this->input->post('username');
$email=$this->input->post('email');
$password=$this->input->post('password');
$group=$this->input->post('group');
$this->model_regis->tambah($username,$email,$password,$group);
redirect('regis/member');
}
public function member(){
$this->load->view('form_regis');
}
public function tambah(){
$this->load->view('form_login');
}
}
?>
3. Membuat file Model_regis.php di dalam folder C:\xampp\htdocs\toko-online\application\models.
<?php
class Model_regis extends CI_Model{
public function tambah($username, $email, $password, $group){
$data = array(
'username' => $username,
'email' => $email,
'password' => $password,
'group' => $group,
);
$this->db->insert('users',$data);
}
}
?>
4. Membuat file form_regis.php di dalam folder C:\xampp\htdocs\toko-online\application\views.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toko Online | Inwepo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<style>
.register{
margin-left: 17%;
}
</style>
</head>
<body>
<?php $this->load->view('layout/top_menu') ?>
<div><?=validation_errors()?></div>
<div><?=$this->session->flashdata('error')?></div>
<?=form_open('regis', ['class'=>'form-horizontal'])?>
<div class="form-group">
<label class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Group</label>
<div class="col-sm-10">
<select name="group">
<option>--Pilih--
<option value="2">Member
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
<div class="register">
<a href="<?php echo base_url();?>index.php/login"><button class="btn btn-warning">Back to Login</button></a>
</div>
</body>
</html>
Nah, untuk melihat tampilan dari menu register ini, pembaca dapat mengetikan URL http://localhost/toko-online/index.php/regis. Berikut ini tampilan dari register:

5. Membuat file Login.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->form_validation->set_rules('username','Username','required|alpha_numeric');
$this->form_validation->set_rules('password','Password','required|alpha_numeric');
if($this->form_validation->run() == FALSE)
{
$this->load->view('form_login');
} else {
$this->load->model('model_users');
$valid_user = $this->model_users->check_credential();
if($valid_user == FALSE)
{
$this->session->set_flashdata('error','Wrong Username / Password!');
redirect('login');
} else {
// if the username and password is a match
$this->session->set_userdata('username', $valid_user->username);
$this->session->set_userdata('group', $valid_user->group);
switch($valid_user->group){
case 1 : //admin
redirect('admin/products');
break;
case 2 : //member
redirect(base_url());
break;
default: break;
}
}
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
}
6. Membuat file Model_users di dalam folder C:\xampp\htdocs\toko-online\application\models.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_users extends CI_Model {
public function check_credential()
{
$username = set_value('username');
$password = set_value('password');
$hasil = $this->db->where('username', $username)
->where('password', $password)
->limit(1)
->get('users');
if($hasil->num_rows() > 0){
return $hasil->row();
} else {
return array();
}
}
}
7. Membuat file form_login.php di dalam folder C:\xampp\htdocs\toko-online\application\views.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toko Online | Inwepo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<style>
.register{
margin-left: 17%;
}
</style>
</head>
<body>
<?php $this->load->view('layout/top_menu') ?>
<div><?=validation_errors()?></div>
<div><?=$this->session->flashdata('error')?></div>
<?=form_open('login', ['class'=>'form-horizontal'])?>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
<div class="register">
<a href="<?php echo base_url();?>index.php/regis"><button class="btn btn-danger">Register</button></a>
</div>
</body>
</html>
Setelah terbentuk file diatas, pembaca dapat melakukan tahap uji coba dengan cara mengetikan URL http://localhost/toko-online/index.php/login. Berikut ini tampilan form login tersebut.

PEMBUATAN  DASHBOARD ORDER MEMBER


1. Membuat file Order.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('username'))
{
redirect('login');
}
$this->load->model('model_orders');
}
public function index()
{
$is_processed = $this->model_orders->process();
if($is_processed){
$this->cart->destroy();
redirect('order/success');
} else {
$this->session->set_flashdata('error','Failed to processed your order, please try again!');
redirect('welcome/cart');
}
}
public function success()
{
$this->load->view('order_success');
}
}
2. Membuat file order_success.php di dalam folder C:\xampp\htdocs\toko-online\application\views.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toko Online | Inwepo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<?php $this->load->view('layout/top_menu') ?>
<p>Thank you, your order is being processed..</p>
</body>
</html>


Setelah member memilih produk yang diinginkan, maka mereka dapat melakukan tahap selanjutya yaitu pemesanan barang dengan cara menekan tombol berwarna hijau yaitu Check Out. Bila ingin menghapus list daftar belanja, member dapat menekan tombol clear cart. Begitu pula bila member masih ingin menambahkan list belanjaan, mereka dapat menekan tombol Continue Shopping.
Nah, ketika data berhasil diproses oleh sistem dan telah masuk kedalam database, maka akan muncul pesan seperti berikut:

MEMBUAT SESSION LOGOUT MULTI-LEVEL ADMIN DAN MEMBER


1. Buka file Login.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers. Kemudian tambahkan function logout seperti dibawah ini:
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
2. Buka file top_menu.php di dalam folder C:\xampp\htdocs\toko-online\application\views. Kemudian tambahkan potongan program berikut:
<?php if($this->session->userdata('username')) { ?>
<li><div style="line-height:50px;">You Are : <?=$this->session->userdata('username')?></div></li>
<li><?php echo anchor('logout', 'Logout');?></li>
<?php } else { ?>
<li><?php echo anchor('login', 'Login');?></li>
<?php } ?>
Screenshot Menu Logout Member:

Screenshot Menu Logout Admin:

Ketika memilih Menu Logout, maka admin maupun member akan diarahkan menuju halaman login seperti gambar dibawah ini:

CRUD ADMIN

READ (MENAMPILKAN)  PRODUK DI HALAMAN ADMIN

1. Buat folder baru bernama backend di dalam C:\xampp\htdocs\toko-online\application\views.

2. Buat file admin_menu.php di dalam folder C:\xampp\htdocs\toko-online\application\views\backend.

<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?=anchor(base_url(), 'Toko Online', ['class'=>'navbar-brand'])?>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php if($this->session->userdata('username')) : ?>
<ul class="nav navbar-nav navbar-left">
<li><?=anchor('admin/products','Products')?></li>
<li><?=anchor('admin/invoices','Invoices')?></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<span style="line-height:50px;">
<?php echo 'You Are: ' . $this->session->userdata('username');?>
</span>
</li>
<li>
<?php echo anchor('logout', 'Logout');?>
</li>
</ul>
<?php endif; ?>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

3. Buat folder bernama admin di dalam C:\xampp\htdocs\toko-online\application\controllers, kemudian buat file Products.php di dalam folder tersebut.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends CI_Controller {
public function __construct(){
parent::__construct();
if($this->session->userdata('group') != '1'){
$this->session->set_flashdata('error','Sorry, you are not logged in!');
redirect('login');
}
//load model -> model_products
$this->load->model('model_products');
}
public function index()
{
$data['products'] = $this->model_products->all();
$this->load->view('backend/view_all_products', $data);
}
}
4. Buat file Model_products.php di dalam folder C:\xampp\htdocs\toko-online\application\models kemudian ketikan potongan program berikut:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_products extends CI_Model {
public function all(){
//query semua record di table products
$hasil = $this->db->get('products');
if($hasil->num_rows() > 0){
return $hasil->result();
} else {
return array();
}
}
public function find($id){
//Query mencari record berdasarkan ID-nya
$hasil = $this->db->where('id', $id)
->limit(1)
->get('products');
if($hasil->num_rows() > 0){
return $hasil->row();
} else {
return array();
}
}
}
Screenshot Menampilkan Produk Penjualan di Halaman Admin

Menampilkan Daftar Produk Penjualan Toko Online di Halaman Admin

CREATE (MENAMBAHKAN) PRODUK DI HALAMAN ADMIN
1. Buat folder uploads di dalam C:\xampp\htdocs\toko-online. Folder ini berfungsi untuk menyimpan gambar hasil upload pada saat menambahakn produk baru.
2. Tambahkan function create pada file Products.php yang terdapat di folder C:\xampp\htdocs\toko-online\application\controllers.
public function create(){
//form validation sebelum mengeksekusi QUERY INSERT
$this->form_validation->set_rules('name', 'Product Name', 'required');
$this->form_validation->set_rules('description', 'Product Description', 'required');
$this->form_validation->set_rules('price', 'Product Price', 'required|integer');
$this->form_validation->set_rules('stock', 'Available Stock', 'required|integer');
//$this->form_validation->set_rules('userfile', 'Product Image', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('backend/form_tambah_product');
} else {
//load uploading file library
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '300'; //KB
$config['max_width'] = '2000'; //pixels
$config['max_height'] = '2000'; //pixels
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//file gagal diupload -> kembali ke form tambah
$this->load->view('backend/form_tambah_product');
} else {
//file berhasil diupload -> lanjutkan ke query INSERT
// eksekusi query INSERT
$gambar = $this->upload->data();
$data_product = array(
'name' => set_value('name'),
'description' => set_value('description'),
'price' => set_value('price'),
'stock' => set_value('stock'),
'image' => $gambar['file_name']
);
$this->model_products->create($data_product);
redirect('admin/products');
}
}
}
3. Tambahkan function create pada file Model_products.php yang terdapat di C:\xampp\htdocs\toko-online\application\models.
public function create($data_products){
//Query INSERT INTO
$this->db->insert('products', $data_products);
}
4. Buat file form_tambah_product.php di dalam folder C:\xampp\htdocs\toko-online\application\views\backend.
<!doctype html>
<html>
<head>
<title>Admin Page | Add New Product</title>
<!-- Load JQuery dari CDN -->
<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- Load DataTables dan Bootstrap dari CDN -->
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.css">
</head>
<body>
<!-- dalam div row harus ada col yang maksimum nilainya 12 -->
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<h1>Add New Product</h1>
<div><?= validation_errors() ?></div>
<?= form_open_multipart('admin/products/create', ['class'=>'form-horizontal']) ?>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="" value="<?= set_value('name') ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<textarea class="form-control" name="description"><?= set_value('description') ?></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" placeholder="" value="<?= set_value('price') ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Available Stock</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="stock" placeholder="" value="<?= set_value('stock') ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Product Image</label>
<div class="col-sm-10">
<input type="file" class="form-control" name="userfile" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Save</button>
</div>
</div>
<?= form_close() ?>
</div>
<div class="col-md-1"></div>
</div>
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
</body>
</html>
Screenshot Menambahkan Produk Penjualan di Halaman Admin

menambahkan produk penjualan di toko online

UPDATE (MENGEDIT) PRODUK DI HALAMAN ADMIN
1. Tambahkan function update pada file Product.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers\admin.
public function update($id){
$this->form_validation->set_rules('name', 'Product Name', 'required');
$this->form_validation->set_rules('description', 'Product Description', 'required');
$this->form_validation->set_rules('price', 'Product Price', 'required|integer');
$this->form_validation->set_rules('stock', 'Available Stock', 'required|integer');
if ($this->form_validation->run() == FALSE)
{
$data['product'] = $this->model_products->find($id);
$this->load->view('backend/form_edit_product', $data);
} else {
if($_FILES['userfile']['name'] != ''){
//form submit dengan gambar diisi
//load uploading file library
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '300'; //KB
$config['max_width'] = '2000'; //pixels
$config['max_height'] = '2000'; //pixels
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['product'] = $this->model_products->find($id);
$this->load->view('backend/form_edit_product', $data);
} else {
$gambar = $this->upload->data();
$data_product = array(
'name' => set_value('name'),
'description' => set_value('description'),
'price' => set_value('price'),
'stock' => set_value('stock'),
'image' => $gambar['file_name']
);
$this->model_products->update($id, $data_product);
redirect('admin/products');
}
} else {
//form submit dengan gambar dikosongkan
$data_product = array(
'name' => set_value('name'),
'description' => set_value('description'),
'price' => set_value('price'),
'stock' => set_value('stock')
);
$this->model_products->update($id, $data_product);
redirect('admin/products');
}
}
}
2. Tambahkan function update pada file Model_product.php di dalam folder C:\xampp\htdocs\toko-online\application\models.
public function update($id, $data_products){
//Query UPDATE FROM ... WHERE id=...
$this->db->where('id', $id)
->update('products', $data_products);
}
3. Membuat tampilan file form_edit_product.php di dalam folder C:\xampp\htdocs\toko-online\application\views\backend.
<?php
$id = $product->id;
if($this->input->post('is_submitted')){
$name = set_value('name');
$description = set_value('description');
$price = set_value('price');
$stock = set_value('stock');
} else {
$name = $product->name;
$description = $product->description;
$price = $product->price;
$stock = $product->stock;
}
?>
<!doctype html>
<html>
<head>
<title>Admin Page | Edit Product</title>
<!-- Load JQuery dari CDN -->
<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- Load DataTables dan Bootstrap dari CDN -->
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.css">
</head>
<body>
<!-- dalam div row harus ada col yang maksimum nilainya 12 -->
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<h1>Edit Product</h1>
<div><?= validation_errors() ?></div>
<?= form_open_multipart('admin/products/update/' . $id, ['class'=>'form-horizontal']) ?>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="" value="<?= $name ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<textarea class="form-control" name="description"><?= $description ?></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" placeholder="" value="<?= $price ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Available Stock</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="stock" placeholder="" value="<?= $stock ?>">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Product Image</label>
<div class="col-sm-10">
<input type="file" class="form-control" name="userfile" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="is_submitted" value="1" />
<button type="submit" class="btn btn-default">Save</button>
</div>
</div>
<?= form_close() ?>
</div>
<div class="col-md-1"></div>
</div>
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
</body>
</html>
Screenshot Mengedit Produk Penjualan di Halaman Admin

mengedit produk penjualan toko online

DELETE (MENGHAPUS) PRODUK DI HALAMAN ADMIN
1. Tambakan function delete pada file products.php di dalam folder C:\xampp\htdocs\toko-online\application\controllers\admin.
public function delete($id){
$this->model_products->delete($id);
redirect('admin/products');
}
2. Tambahkan function delete di file Model_products.php di dalam folder C:\xampp\htdocs\toko-online\application\models.
public function delete($id){
//Query DELETE ... WHERE id=...
$this->db->where('id', $id)
->delete('products');
}


Komentar

Postingan populer dari blog ini

DOTA 2

TPG _ Review Game DOTA 2 Klasifikasi Game Genre : MOBA (Multiplayer Online Battle Arena) , Strategy Tema : Fantasy Rating : No Rating (ESRB) Metacritic Rating : 90 Publisher : Valve Corporation Release Date : July 9, 2013 Deskripsi Dota 2 adalah video game multiplayer online battle arena (MOBA) 2013 yang dikembangkan dan diterbitkan oleh Valve. Gim ini merupakan sekuel Defense of the Ancients (DotA), yang merupakan mod buatan komunitas untuk Warcraft III: Reign of Chaos dari Blizzard Entertainment. Dota 2 dimainkan dalam pertandingan antara dua tim yang terdiri dari lima pemain, dengan masing-masing tim menempati dan mempertahankan markas mereka sendiri di peta. Masing-masing dari sepuluh pemain secara independen mengontrol karakter kuat yang dikenal sebagai "pahlawan" yang semuanya memiliki kemampuan unik dan gaya permainan yang berbeda. Selama pertandingan, pemain mengumpulkan poin pengalaman dan item untuk pahlawan mereka agar berhasil mengalahkan pahlawan tim lawan...

Fit For The Future : Kuliah Tamu 27 Agustus 2019

Review Kuliah Tamu Kuliah tamu hari Selasa, 27 Agusutus 2019 bertema “Fit for The Future”.  Pembicara dari kuliah tamu ini adalah Bapak Ahmadsyah Alghozi. Bapak Ahmadsyah memiliki segudang pengalaman dalam segala bidang. Bapak ini sudah memiliki karir yang bagus baik di Indonesia, maupun di kancah internasional. Dalam kesempatan ini, Bapak Ahmadsyah membagikan berbagai pengalaman yang ia dapat selama hidupnya. Beliau pernah bekerja di landmark Indonesia, lalu melanjutkan ke landmark Belanda, dan mengimplementasikan software di sana. Pada tahun 2011, ia bergabung di perusahaan petroleum, kemudian pindah ke Shell menjabat sebagai technical data consultant. Setelah merasakan menjadi user akhirnya kembali lagi kedalam dunia IT dimana pengalaman disaat beliau menjadi user membantu dalam membuka perspektif yang baru lagi. Terdapat dua tipe orang dalam suatu pekerjaan, yaitu engineer dan scientist. Keduanya memiliki cara bekerja yang berbeda. Seorang engineer membuat sesuatu...