//

Script PHP Web Jual Beli

Tugas Pemrograman Web Dinamis ke 2

Ada 3 langkah dalam pembuatan web toko online, yang pertama membuat database, yang kedua membuat php dan yang ke tiga membuat atau mencari css yang sudah tersedia. Ayo kita lihat hasil nya 


LANGKAH 1 : Membuat Database










LANGKAH 2 DAN 3 : Membuat php DAN CSS

input.php

<?php
session_start();
error_reporting(0);
include "include/lib.php";
$input=$_GET[input];
$sid = session_id();
$inputform=$_GET[inputform];

if($input=='add'){
                $sql = mysql_query("SELECT id_product FROM keranjang WHERE id_product='$_GET[id]' AND id_session='$sid'");
                $num = mysql_num_rows($sql);
                if ($num==0){
                                mysql_query("INSERT INTO keranjang(id_product,
                                                                                                                                                                                id_session,
                                                                                                                                                                                tgl_keranjang,
                                                                                                                                                                                qty)
                                                                                                                                                VALUES                ('$_GET[id]',
                                                                                                                                                                                '$sid',
                                                                                                                                                                                '$tgl_sekarang',
                                                                                                                                                                                '1')") or die (mysql_error());
                }
                else {
                                mysql_query("UPDATE keranjang SET qty = qty + 1 WHERE id_session = '$sid' AND id_product='$_GET[id]'") or die (mysql_error());
                }
                deletecart();
                header('location:index.php?v=cart');
                }                                                             
elseif ($input=='delete'){
                mysql_query("DELETE FROM keranjang WHERE id_keranjang='$_GET[id]'");
                header('location:index.php?v=cart');
                }
elseif ($input=='inputform'){
                $ct_content = cart_content();
                $jml = count($ct_content);
                $now = date("Ymd");
                for($i=0; $i<$jml; $i++){
                mysql_query("INSERT INTO order_product(name,
                                                                                                                                                                                email,
                                                                                                                                                                                phone,
                                                                                                                                                                                address,
                                                                                                                                                                                id_product,
                                                                                                                                                                                jumlah,
                                                                                                                                                                                tanggal,
                                                                                                                                                                                id_pemesan)
                                                                                                                                                VALUES ('$_POST[name]',
                                                                                                                                                                                '$_POST[email]',
                                                                                                                                                                                '$_POST[telp]',
                                                                                                                                                                                '$_POST[address]',
                                                                                                                                                                                {$ct_content[$i]['id_product']},
                                                                                                                                                                                {$ct_content[$i]['qty']},
                                                                                                                                                                                '$now',
                                                                                                                                                                                '$sid')");
                                }
                for($i=0; $i<$jml; $i++){
                                mysql_query("DELETE FROM keranjang WHERE id_keranjang = {$ct_content[$i]['id_keranjang']}");
                                }
                                echo "<script>window.alert('Terima Kasih Pesanan Anda Sedang Kami Proses');
        window.location=('index.php')</script>";
                }                                                                                                                                                                                                                                                                             
?>

index.php

<?php
include "include/lib.php";
include "include/top.php";
include "include/left.php";
                $v = $_GET['v'];
                if($v == 'produk') {
                                require_once "include/product.php";
                } elseif ($v == 'cart') {
                                require_once "include/cart.php";
                } elseif ($v == 'order') {
                                require_once "include/order.php";
                } else {
                                require_once "include/home.php";
                }
include "include/bottom.php";
?>

home.php

<div class="BigContent">
<div class="RightContent">
<h1 class="Judul">Produk terbaru</h1>
<?php
                $sql = mysql_query("SELECT * FROM product ORDER BY id DESC") or die ("Query gagal dengan error: ".mysql_error());
                while($data=mysql_fetch_array($sql)){ ?>
<div class="produk">
                <a href="?v=produk&id=<?php echo $data['id']; ?>">
                                <img title="<?php echo $data['product_name']; ?>" class="FotoProduk" src="foto/<?php echo $data['image']; ?>" height="110px" />
                </a>
                <br class="clearfloat" />
                <div class="KotakKet">
                <a class="pesanprod" href="input.php?input=add&id=<?php echo $data['id']; ?>">Pesan</a>
                <a class="detprod" href="product.php?id=<?php echo $data['id']; ?>">Detail</a>
                </div>
</div>
<?php } ?>

left.php

<div class="LeftContent">
                <div id="navigation">
                                <ul class="top-level">
                                <?php
                                                $kat = mysql_query("SELECT category, category.id from category join product on product.id_category=category.id group by category");
                                                while($list=mysql_fetch_array($kat)){
                                                                echo"<li><a href='?v=produk&cat=$list[id]'>$list[category]</a></li>";
                                                }
                                ?>
                                </ul>
                </div>
</div>

top.php

<?php
                error_reporting(0);
                session_start();
                ?>
<html>
                <head>
                                <title>Toko Online</title>
                                <link rel="StyleSheet" href="css/style.css" type="text/css" />
                                <link rel="StyleSheet" href="css/reset.css" type="text/css" />
                </head>
               
                <body>
                                <div class="wrap">
                                                <div class="header">
                                                                <div class="LeftOne">
                                                                                <a href="index.php"><img width="150px" src="images/toko-online.png" /></a>
                                                                </div>
                                                                <div class="RightOne">
                                                                                <div class="cart">
                                                                                                <?php include "cart2.php"; ?>
                                                                                </div>
                                                                                <br class="clearfloat" />
                                                                                <a class="tocart" href="cart.php">Keranjang</a>
                                                                </div>
                                                </div>
                                                <br class="clearfloat" />

lib.php

<?php
$seminggu = array("Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
$hari = date("w");
$hari_ini = $seminggu[$hari];

$tgl_sekarang = date("Ymd");
$thn_sekarang = date("Y");
$jam_sekarang = date("H:i:s");

$nama_bln=array(1=> "Januari", "Februari", "Maret", "April", "Mei",
                    "Juni", "Juli", "Agustus", "September",
                    "Oktober", "November", "Desember");
//fungsi untuk keranjang belanja
function cart_content(){
                $ct_content = array();
                $sid = session_id();
                $sql = mysql_query("SELECT * FROM keranjang WHERE id_session='$sid'");
                while ($r=mysql_fetch_array($sql)) {
                                $ct_content[] = $r;
                }
                return $ct_content;
}
//fungsi untuk menghapus keranjang
function deletecart(){
                $del = date('Y-m-d', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
                mysql_query("DELETE FROM keranjang WHERE tgl_keranjang < '$del'");
                }
                $server = "localhost";
                $username = "root";
                $password = "";
                $database = "fadmalapwd2";

                mysql_connect($server,$username,$password) or die("Koneksi gagal");
                mysql_select_db($database) or die("Database tidak bisa dibuka");
?>

product.php

<div class="BigContent">
<div class="RightContent">
<?php
                $prod = $_GET['id'];
                $cat = $_GET['cat'];
                if($cat){
                $sql = mysql_query("SELECT * FROM category WHERE id = '$cat'");
                $jdl = mysql_fetch_array($sql);
                echo "<h1 class='Judul'>Kategori $jdl[category]</h1>";
                $sql2 = mysql_query("SELECT * FROM product WHERE id_category='$cat'");
                while($t = mysql_fetch_array($sql2)){ ?>
                <div class="produk">
                                <a href="?v=produk&id=<?php echo $t['id']; ?>">
                                                <img title="<?php echo $t['product_name']; ?>" class="FotoProduk" src="foto/<?php echo $t['image']; ?>" height="110px" />
                                </a>
                                <br class="clearfloat" />
                                <div class="KotakKet">
                                <a class="pesanprod" href="input.php?input=add&id=<?php echo $t['id']; ?>">Pesan</a>
                                <a class="detprod" href="?v=produk&id=<?php echo $t['id']; ?>">Detail</a>
                                </div>
                </div>                                                                                  
<?php   } } elseif($prod){ ?>
<?php
                $sql = mysql_query("SELECT * FROM product WHERE id='$prod'");
                $d = mysql_fetch_array($sql);
                ?>
                <h1 class="Judul">Produk <?php echo $d['product_name']; ?></h1>
                <div class="KetProd">
                                <img class="GambarKetProd" src="foto/<?php echo $d['image']; ?>">
                                <?php echo $d['deskripsi']; ?>
                </div>
                <a class="haha" href="javascript:history.go(-1)">Kembali</a> | <a class="haha" href="input.php?input=add&id=<?php echo $d['id']; ?>">Beli</a>
<?php } ?>

bottom.php

                                                </div>
                                                                <br class="clearfloat" />
                                                </div>
                                                <div class="footer">
                                                                <p align="center">&copy; <?php echo date('Y') ?> Fadmala Anggriana <a href="#">11018014</a></p>
                                                </div>
                                </div>
                </body>
</html>

logout.php

<?php
  session_start();
  session_destroy();
  echo "<script>window.alert('Kamu telah berhasil keluar dari sistem administrator');
        window.location=('../index.php')</script>";
?>

aksi.php

<?php
session_start();
error_reporting(0);
include "../include/lib.php";

$mod=$_GET[mod];
$act=$_GET[act];


// Menghapus data
if (isset($mod) AND $act=='hapus'){
  mysql_query("DELETE FROM ".$mod." WHERE id ='$_GET[id]'");
  header('location:admin.php?mod='.$mod);
}


//Add Category
elseif ($mod=='category' AND $act=='input'){
                $insert = mysql_query("INSERT INTO category (id,category) VALUES ('','$_POST[nama_kategori]')");
                if($insert == FALSE){
                                echo "<p>Kategori gagal ditambahkan, alesannya:".(mysql_error())."</p>";
                                }
                header('location:admin.php?mod='.$mod);
                }
//Category Update
elseif ($mod=='category' AND $act=='update'){
                $update = mysql_query("UPDATE category SET category = '$_POST[nama_kategori]' WHERE id = '$_POST[id]'");
                if($update ==FALSE){
                                echo "<p>Update gagal dilakukan karena:".(mysql_error())."</p>";
                                }
                header('location:admin.php?mod='.$mod);
                }
               
//Add Product
elseif ($mod=='product' AND $act=='input'){
$lokasi_file         = $_FILES['fgambar']['tmp_name'];
$tipe_file                             = $_FILES['fgambar']['type'];
$nama_file                         = $_FILES['fgambar']['name'];

move_uploaded_file($lokasi_file,"../foto/$nama_file");
                $insert = mysql_query("INSERT INTO product (id,
                                                                                                                                                                                                product_name,
                                                                                                                                                                                                price,
                                                                                                                                                                                                image,
                                                                                                                                                                                                id_category,
                                                                                                                                                                                                deskripsi)
                                                                                                                                                                VALUES ('$_POST[product_name]',
                                                                                                                                                                                                '$_POST[price]',
                                                                                                                                                                                                '$nama_file',
                                                                                                                                                                                                '$_POST[cat]',
                                                                                                                                                                                                '$_POST[deskripsi]')");
                                                                                                                                                                                               
                header('location:admin.php?mod='.$mod);
}
//Product Update
elseif ($mod=='product' AND $act=='update'){
                $lokasi_file         = $_FILES['fgambar']['tmp_name'];
                $tipe_file                             = $_FILES['fgambar']['type'];
                $nama_file                         = $_FILES['fgambar']['name'];

                //If the image doesnt change
                if (empty($lokasi_file)){
                                mysql_query("UPDATE product SET product_name                         = '$_POST[product_name]',
                                                                                                                                                                price                      = '$_POST[price]',
                                                                                                                                                                id_category        = '$_POST[cat]',
                                                                                                                                                                deskripsi              = '$_POST[deskripsi]'
                                                                                                                                                WHERE id = '$_POST[id]'");
                }
                else {
                                move_uploaded_file($lokasi_file,"../foto/$nama_file");
                                mysql_query("UPDATE product SET product_name= '$_POST[product_name]',
                                                                                                                                                                price                      = '$_POST[price]',
                                                                                                                                                                image    = '$nama_file',
                                                                                                                                                                id_category        = '$_POST[cat]',
                                                                                                                                                                deskripsi              = '$_POST[deskripsi]'
                                                                                                                                                WHERE id = '$_POST[id]'");
                }
                header('location:admin.php?mod='.$mod);
}
?>

admin.php

<?php
include "../include/lib.php";
error_reporting(0);
session_start();
if (empty($_SESSION[namauser]) AND empty($_SESSION[passuser])){
  echo "<link href='css/style.css' rel='stylesheet' type='text/css'>
 <center>Silahkan Login terlebih dahulu <br>";
  echo "<a href=index.php><b>LOGIN</b></a></center>";
}
else{
?>

<html>
<head>
<title>:: Toko Online ::</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="nicedit/nicEdit.js" type="text/javascript"></script>
<script src="js/jquery.js" type="text/javascript"></script>
        <script src="media/js/jquery.dataTables.js" type="text/javascript"></script>
        <style type="text/css">
            @import "media/css/demo_table_jui.css";
            @import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
        </style>
       
        <style>
            *{
                font-family: arial;
            }
        </style>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){
                $('#datatables').dataTable({
                    "sPaginationType":"full_numbers",
                    "aaSorting":[[2, "desc"]],
                    "bJQueryUI":false
                });
                $('#pengelola').dataTable({
                    "sPaginationType":"full_numbers",
                    "aaSorting":[[2, "desc"]],
                    "bJQueryUI":false
                });
            })
           
        </script>
<script type="text/javascript">
                bkLib.onDomLoaded(function(){
                                nicEditors.allTextAreas(({buttonList : ['fontSize','bold','italic','underline','strikeThrough','subscript','superscript','html','image']})) });
</script>
</head>
<body>
                                <div class="wrap">
                                                <div class="header">
                                                                <div class="LeftOne">
                                                                                <a href="index.php">
                                                                                </a>
                                                                </div>
                                                                <div class="RightOne">
                                                                                <div class="cart">
                                                                                                <span class="KetCart">Administrator</span>
                                                                                </div>
                                                                </div>
                                                </div>
                                                <br class="clearfloat" />
                <div class="BigCOntent">
                                <div class="LeftContent">
                                                <div id="navigation">
                                                  <ul class="top-level">
                                                                <li><a href="?mod=home">Home</a></li>
                                                                <li><a href="?mod=product">Produk</a></li>
                                                                <li><a href="?mod=category">Kategori</a></li>
                                                                <li><a href="?mod=report">Report</a></li>
                                                                <li><a href=logout.php>Logout</a></li>
                                                  </ul>
                                                </div>
                                </div>
                                <div class="RightContent">
                                                <?php
                                                                if ($_GET[mod]=='home'){
                                                                  echo "<h1 class='Judul'>Selamat Datang</h1>
                                                                                                  Anda Telah masuk ke halaman administrator silahkan gunakan menu yang tersedia :)</p>";
                                                                }
                                                                //Add Kategori
                                                                elseif ($_GET[mod]=='category'){
                                                                                require_once "modul/mod_kategori.php";
                                                                                }
                                                                //Add Product
                                                                elseif ($_GET[mod]=='product'){
                                                                                require_once "modul/mod_produk.php";
                                                                                }
                                                                //Report
                                                                elseif ($_GET[mod]=='report'){
                                                                                require_once "modul/report.php";
                                                                                }
                                                ?>
                                </div>
                </div>
                                                                <br class="clearfloat" />
                                                <div class="footer">
                                                                <p align="center">&copy; <?php echo date('Y') ?> Fadmala Anggriana <a href="#">11018014</a></p>
                                                </div>
                                </div>
                </body>
</html>
<?php
}
?>

login.php

<?php
include "../include/lib.php";
error_reporting(0);
$pass=md5($_POST[password]);

$login=mysql_query("SELECT * FROM user WHERE id_user='$_POST[username]' AND password='$pass'");
$ketemu=mysql_num_rows($login);
$r=mysql_fetch_array($login);
//echo mysql_error();
//echo "jjjjjjj";
//die($ketemu);
// Apabila username dan password ditemukan
if ($ketemu > 0){
  session_start();
  //session_register("passuser");
  //session_register("leveluser");

  $_SESSION[passuser] = $r[password];
  $_SESSION[leveluser]= $r[level];
  header('location:admin.php?mod=home');
}
else{
  echo "<script>window.alert('Username atau Password Salah!!!');
        window.location=('admin.php')</script>";
}
?>




HASIL FOTO :


















Posting Komentar

7 Komentar

  1. smua scrip php di simpen di file apa ya mas ??

    BalasHapus
  2. gada link downloadnya apa ni yg udah lengkap di zip dari sql sampe php nya bro..?

    BalasHapus
  3. susah.. error semua... minta zip nya donk.. biar gampang.. sekalian sama databasenya...

    BalasHapus
  4. susah gan. pakai cara instan aja lebih komplit dan cantik, beli script aja sama orang..
    atau pakai cms osclass sudah bisa..

    BalasHapus
  5. sangat berguna, thanks gan
    info nya buat file zip kalo ada saya minta

    BalasHapus
  6. makasih banyak sangat sangat membantu dan bermanfaat

    BalasHapus
  7. sederhana tapi komplit, jadi pingin nyoba terapin, file php-nya di share dong mas, biar gak ketemu banyak error :D

    BalasHapus