FP PBKK - Pembuatan Website Toko Online
Anggota:
Aulia Ihza Hendradi - 05111840000089
R Dafa Berlian Denmar - 05111840000149
Alberto Sanjaya - 05111840000150
Tugas FP pada kali ini, kami membuat website toko yang berfokus pada penjualan
alat-alat elektronik. Pada website ini terdapat 2 jenis user yaitu admin dan
juga pengguna.
Link Github:
https://github.com/aulkiller/olshop ( SQL Database terdapat di direktori database )
Username Admin: admin
Password Admin: 12345678
Berikut Tampilan dari website online yang kami buat.
Tampilan Halaman Utama Website
Register dan Login Member
Order oleh Member
1. Tampilan dalam Keranjang
2. Tampilan Checkout
3. Tampilan List Pesanan yang pernah dilakukan
4. Tampilan Detail Pesanan
Logout Session Admin dan User
1. Logout Dashboard Admin
2. Logout User
Halaman Admin - Produk
1. Kelola Produk
2. Tambah Produk
Halaman Admin - Invoice
1. Tampilan List Pesanan
2. Tampilan Detail Pesanan
Revisi Website Setelah Demo
Perubahan Kode
Pada Product
...
$data["short_desc"] = $this->input->post('product_short_description');
$data["stock"] = $this->input->post('product_stock'); //line baru
$data["category_id"] = $this->input->post('product_category');
...
Pada Cart
...
public function addProductToCart($cart_id, $product_id, $quantity) {
$array = array (
"cart_id" => $cart_id,
"product_id" => $product_id,
"quantity" => $quantity
);
if($this->UpdateStock($product_id, $quantity)){ //if baru jika barang masuk keranjang
return $this->db->insert("product_cart_table", $array);
}
}
private function UpdateStock($product_id, $stock){ //kurangi stock
$this->db->set('stock', 'stock - '.$stock, FALSE);
$this->db->where('product_id', $product_id);
return $this->db->update('product_table');
}
public function removeFromCart($pcid) {
$getto = $this->db->get_where("product_cart_table pct", array("pct.product_cart_id" => $pcid))->row();
if($this->ResetStock($getto->product_id, $getto->quantity)){ //if baru jika barang dihapus dr keranjang
return $this->db->where('product_cart_id', $pcid)->delete(PRODUCTCART);
}
}
private function ResetStock($product_id, $stock){ //reset stock ke semula
$this->db->set('stock', 'stock + '.$stock, FALSE);
$this->db->where('product_id', $product_id);
return $this->db->update('product_table');
}
...
Perubahan Database
Adanya stock pada product_table dengan unsigned smallint
Tampilan Website
(Ada stok pada Halaman Utama-Produk-Tambah Produk-Edit Produk)
Tugas FP PBKK - Online Shop Code Igniter
1.Persiapan
- Menginstall PHP development environtment, contohnya XAMPP (apachefriends.org)
- Laravel 7 di Installation - Laravel - The PHP Framework For Web Artisans
- Membuka link petunjuk yang ada di Halo Bro: Membuat Halaman Web Pertama Dengan Laravel | ID Laravel (id-laravel.com)
- dan Membuat Form Registrasi Dengan Laravel | ID Laravel (id-laravel.com)
- Mount Folder Project di Windows Terminal
- Menginstall Composer (composer install)
- Menginstall Node Module (npm install && npm run dev)
- Membuat database bernama kreativ di MySQL XAMPP
- Di Windows terminal lagi
- Menjalankan migrasi database (php artisan migrate)
- Menjalankan Web di Lokal (php artisan serve)
2.Program Kreativ Company Profile
Setelah php artisan serve dijalankan secara default web dapat dilihat di http://127.0.0.1:8000/. Buat kontroller untuk CRUD dan view profile,menggunakan syntax
Jika ada crud -> php artisan make:controller [namakontroller] --resource --model=[namamodel]
Jika hanya view -> php artisan make:controller [namakontroller]
Jika model belum ada,akan dibuatkan sendiri sesuai dengan model yang kita definisikan.
Contoh Isi dari Controller untuk view halaman beranda,kreativ,karir, dan partner:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index(){
return view('pages.index');
}
public function kreativ(){
return view('pages.kreativ');
}
public function undercon(){
return view('pages.undercon');
}
public function karir(){
return view('pages.karir');
}
}
Contoh Isi dari Controller untuk CRUD halaman CRUD Reach Us:
<?php
namespace App\Http\Controllers;
use App\reachus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ReachusController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$reachsus = reachus::orderBy('created_at', 'desc')->get();
return view('reachus.index', compact('reachsus'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('reachus.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'phone' => 'required',
'email' => 'required'
]);
//create req
$reachsus = new reachus;
$reachsus->name = $request->input('name');
$reachsus->company = $request->input('company');
$reachsus->phone = $request->input('phone');
$reachsus->email = $request->input('email');
$reachsus->time = $request->input('time');
$reachsus->date = $request->input('date');
$reachsus->desc = $request->input('desc');
$reachsus->save();
return redirect('reachus/create')->with('Success',"Permintaan Baru Berhasil Ditambah");
}
/**
* Display the specified resource.
*
* @param \App\reachus $reachus
* @return \Illuminate\Http\Response
*/
public function show(reachus $reachus)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\reachus $reachus
* @return \Illuminate\Http\Response
*/
public function edit(reachus $reachus)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\reachus $reachus
* @return \Illuminate\Http\Response
*/
public function update(Request $request, reachus $reachus)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\reachus $reachus
* @return \Illuminate\Http\Response
*/
public function destroy(reachus $reachus)
{
//
}
}
Untuk view dapat diatur pada Direktori-xampp\htdocs\[namaprogram]\resources\views
Untuk yang seputaran CRUD mewakili folder reachus
Untuk yang seputaran view non CRUD diwakili folder pages
Mengatur routing pada Direktori-xampp\htdocs\[namaprogram]\routes\web.php
...
Route::get('/', 'HomeController@index');
Route::get('/KreativMorePage', 'HomeController@kreativ');
Route::get('/Karir', 'HomeController@karir');
Route::get('/Undercon', 'HomeController@undercon');
Route::resource('reachus', 'ReachusController');
...
Isi model reachus di Direktori-xampp\htdocs\[namaprogram]\app\reachus.php yang digenerate sebelumnya
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class reachus extends Model
{
protected $table = 'reachus';
public $primaryKey = 'id_req';
}
Isi migrasi pada pembuatan tabel reachus (setelah diisi lakukan migrate kembali sesuai persiapan)
...
public function up()
{
Schema::create('reachus', function (Blueprint $table) {
$table->id("id_req");
$table->string('name');
$table->string('company');
$table->string('phone');
$table->string('email');
$table->time('time');
$table->date('date');
$table->longText('desc');
$table->timestamps();
});
}
public function down()
{
Schema::table('reachus', function (Blueprint $table) {
Schema::dropIfExists('reachus');
});
}
...
Link Github : aulkiller/kreativ (github.com)
Halaman Depan
Halaman Kreativ
Halaman Karir
Halaman Partner(Belum ada Partner)
Halaman ReachUs
Halaman ReachUs untuk view isi request
Tugas 8 PBKK - Laravel Company Profile
1.Persiapan
- Mengikuti petunjuk Tugas 6 pada Tugas 6 PBKK - Pengenalan Framework Code Igniter (iniblogspotaulia.blogspot.com)
- Membuka link petunjuk yang ada di Tutorial Codeigniter #5: Cara Membuat Fitur CRUD yang Benar! (petanikode.com)
- dan Tutorial Codeigniter #6: Membuat Fitur Upload File (petanikode.com)
2.Program T6PBKK dengan CRUD
Setelah halaman CV sudah jadi disini kita dapat mengikuti tutorial petanikoding untuk konfigurasi autoload.php dan database.php yang akan disesuaikan dengan database yang kita buat di MySQL, kita akan membuat databasenya terlebih dahulu di database MySQL dengan nama T6PBKK lalu membuat tabelnya menggunakan syntax
CREATE TABLE `portfolios` (
`portfolio_id` varchar(64) NOT NULL,
`portfolio_name` varchar(100) NOT NULL,
`portfolio_category` varchar(30) NOT NULL,
`portfolio_date` date NOT NULL,
`portfolio_desc` varchar(250) NOT NULL,
`portfolio_image` varchar(250) NOT NULL DEFAULT 'default.jpg'
)
dan modelnya di folder Direktori-xampp\htdocs\T6PBKK[nama program]\application\models\Portfolio_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Portfolio_model extends CI_Model
{
private $_table = "portfolios";
public $portfolio_id;
public $portfolio_name;
public $portfolio_category;
public $portfolio_date;
public $portfolio_desc;
public $portfolio_image = "default.jpg";
public function rules()
{
return [
['field' => 'portfolio_name',
'label' => 'Portfolio Name',
'rules' => 'required'],
['field' => 'portfolio_date',
'label' => 'Portfolio Date',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->order_by('portfolio_date','ASC')->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["portfolio_id" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->portfolio_id = uniqid();
$this->portfolio_name = $post["portfolio_name"];
$this->portfolio_category = $post["portfolio_category"];
$this->portfolio_date = $post["portfolio_date"];
$this->portfolio_desc = $post["portfolio_desc"];
$this->portfolio_image = $this->_uploadImage();
return $this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->portfolio_id = $post["portfolio_id"];
$this->portfolio_name = $post["portfolio_name"];
$this->portfolio_category = $post["portfolio_category"];
$this->portfolio_date = $post["portfolio_date"];
$this->portfolio_desc = $post["portfolio_desc"];
if (!empty($_FILES["portfolio_image"]["name"])) {
print_r($_FILES);
$this->portfolio_image = $this->_uploadImage();
} else {
$this->portfolio_image = $post["old_image"];
}
return $this->db->update($this->_table, $this, array('portfolio_id' => $post['portfolio_id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("portfolio_id" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './upload/portfolio/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->portfolio_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('portfolio_image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
private function _deleteImage($id)
{
$portfolio = $this->getById($id);
if ($portfolio->portfolio_image != "default.jpg") {
$filename = explode(".", $portfolio->portfolio_image)[0];
return array_map('unlink', glob(FCPATH."upload/portfolio/$filename.*"));
}
}
}
lalu update welcome.php pada Direktori-xampp\htdocs\T6PBKK[nama program]\application\controller sehingga halaman portfoliouser dapat menerima data(Read)
...
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("portfolio_model");
$this->load->library('form_validation');
}
...
public function portfoliouser()
{
// fungsi untuk me-load data.php
$data["portfolios"] = $this->portfolio_model->getAll();
$this->load->view('portfoliouser', $data);
}
...
Untuk halaman portofoliouser.php pada Direktori-xampp\htdocs\T6PBKK[nama program]\application\view akan diisi sedemikian rupa dengan header dan footer menyerupai Tugas 6
...
<div class="Title">
<h1>My Portfolio List</h1>
</div>
<div class="subtitle">
<h2>Check it Out</h2>
</div>
<div class="wadah">
<?php foreach ($portfolios as $portfolio): ?>
<div class="part">
<img src="<?php echo base_url('upload/portfolio/'.$portfolio->portfolio_image) ?>">
<div class="details">
<h4><?php echo $portfolio->portfolio_name ?></h4>
<h5>Category : <?php echo $portfolio->portfolio_category ?></h5>
<h6>Date : <?php echo $portfolio->portfolio_date ?></h6>
<p><?php echo $portfolio->portfolio_desc ?></p>
</div>
</div>
<?php endforeach; ?>
</div>
...
menghasilkan
Karena operasi CRUD dalam bentuk tabel akan dipisah dari user view maka akan kita atur melalui admin mengikuti tutorial petani koding dengan modifikasi pada Direktori-xampp\htdocs\T6PBKK[nama program]\controller\admin\Portofolios.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Portfolios extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("portfolio_model");
$this->load->library('form_validation');
}
public function index()
{
$data["portfolios"] = $this->portfolio_model->getAll();
$this->load->view("admin/portfolio/list", $data);
}
public function add()
{
$portfolio = $this->portfolio_model;
$validation = $this->form_validation;
$validation->set_rules($portfolio->rules());
if ($validation->run()) {
$portfolio->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/portfolio/new_form");
}
public function edit($id = null)
{
if (!isset($id)) redirect('admin/portfolios');
$portfolio = $this->portfolio_model;
$validation = $this->form_validation;
$validation->set_rules($portfolio->rules());
if ($validation->run()) {
$portfolio->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$data["portfolio"] = $portfolio->getById($id);
if (!$data["portfolio"]) show_404();
$this->load->view("admin/portfolio/edit_form", $data);
}
public function delete($id=null)
{
if (!isset($id)) show_404();
if ($this->portfolio_model->delete($id)) {
redirect(site_url('admin/portfolios'));
}
}
}
Lalu Atur halaman new_form, edit_form,dan list mengikuti petunjuk dengan modifikasi variabel sesuai nama di model.
new_form(Create)
...
<?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/portfolios/') ?>"><i class="fas fa-arrow-left"></i> Back</a>
</div>
<div class="card-body">
<form action="<?php echo site_url('admin/portfolios/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="portfolio_name">Name*</label>
<input class="form-control <?php echo form_error('portfolio_name') ? 'is-invalid':'' ?>"
type="text" name="portfolio_name" placeholder="Portfolio Name" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_name') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_category">Category</label>
<input class="form-control <?php echo form_error('portfolio_category') ? 'is-invalid':'' ?>"
type="text" name="portfolio_category" placeholder="Portfolio Category" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_category') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_date">Date*</label>
<input class="form-control <?php echo form_error('portfolio_date') ? 'is-invalid':'' ?>"
type="date" name="portfolio_date" placeholder="Portfolio Date" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_date') ?>
</div>
</div>
<div class="form-group">
<label for="name">Photo</label>
<input class="form-control-file <?php echo form_error('portfolio_category') ? 'is-invalid':'' ?>"
type="file" name="image" />
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_desc">Description</label>
<textarea class="form-control <?php echo form_error('portfolio_desc') ? 'is-invalid':'' ?>"
name="portfolio_desc" placeholder="Portfolio description..."></textarea>
<div class="invalid-feedback">
<?php echo form_error('portfolio_desc') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
</div>
...
edit_form(Update)
...
<?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/portfolios/') ?>"><i class="fas fa-arrow-left"></i>
Back</a>
</div>
<div class="card-body">
<form action="" method="post" enctype="multipart/form-data">
<!-- Note: atribut action dikosongkan, artinya action-nya akan diproses
oleh controller tempat vuew ini digunakan. Yakni index.php/admin/portfolios/edit/ID --->
<input type="hidden" name="portfolio_id" value="<?php echo $portfolio->portfolio_id?>" />
<div class="form-group">
<label for="portfolio_name">Name*</label>
<input class="form-control <?php echo form_error('portfolio_name') ? 'is-invalid':'' ?>"
type="text" name="portfolio_name" placeholder="Portfolio Name" value="<?php echo $portfolio->portfolio_name ?>" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_name') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_category">Category</label>
<input class="form-control <?php echo form_error('portfolio_category') ? 'is-invalid':'' ?>"
type="text" name="portfolio_category" placeholder="Portfolio Category" value="<?php echo $portfolio->portfolio_category ?>" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_category') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_date">Date*</label>
<input class="form-control <?php echo form_error('portfolio_date') ? 'is-invalid':'' ?>"
type="date" name="portfolio_date" placeholder="Portfolio Date" value="<?php echo $portfolio->portfolio_date ?>" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_date') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_image">Photo</label>
<input class="form-control-file <?php echo form_error('portfolio_image') ? 'is-invalid':'' ?>"
type="file" name="portfolio_image" />
<input type="hidden" name="old_image" value="<?php echo $portfolio->portfolio_image ?>" />
<div class="invalid-feedback">
<?php echo form_error('portfolio_image') ?>
</div>
</div>
<div class="form-group">
<label for="portfolio_desc">Description</label>
<textarea class="form-control <?php echo form_error('portfolio_desc') ? 'is-invalid':'' ?>"
name="portfolio_desc" placeholder="Portfolio description..."><?php echo $portfolio->portfolio_desc ?></textarea>
<div class="invalid-feedback">
<?php echo form_error('portfolio_desc') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
</div>
...
list(Read)
...
<!-- DataTables -->
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/portfolios/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 width="13%">Name</th>
<th width="13%">Category</th>
<th width="13%">Date</th>
<th width="20%">Photo</th>
<th width="20%">Description</th>
<th width="20%">Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($portfolios as $portfolio): ?>
<tr>
<td>
<?php echo $portfolio->portfolio_name ?>
</td>
<td>
<?php echo $portfolio->portfolio_category ?>
</td>
<td>
<?php echo $portfolio->portfolio_date ?>
</td>
<td>
<img src="<?php echo base_url('upload/portfolio/'.$portfolio->portfolio_image) ?>" style="width:80%" />
</td>
<td>
<?php echo substr($portfolio->portfolio_desc, 0, 120) ?>...</td>
<td width="250">
<a href="<?php echo site_url('admin/portfolios/edit/'.$portfolio->portfolio_id) ?>"
class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
<a onclick="deleteConfirm('<?php echo site_url('admin/portfolios/delete/'.$portfolio->portfolio_id) ?>')"
href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
...
Untuk halaman list tambahkan line berikut dibagian bawah body untuk menampilkan modal Delete tanpa perlu menginclude atau echo file php baru
...
<!-- 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>
<script>
function deleteConfirm(url){
$('#btn-delete').attr('href', url);
$('#deleteModal').modal();
}
</script>
...
Untuk hasil dapat dilihat pada foto berikut ini :
Github : aulkiller/T6PBKK (github.com)