1.
Membuat Database
-
Tabel Untuk Barang
CREATE TABLE IF NOT EXISTS `barang` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`namabarang` varchar(200) NOT NULL,
`harga` varchar(100) NOT NULL,
`stok` int(10) NOT NULL,
`gambar` mediumblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
-
Tabel Untuk Pesan
CREATE TABLE IF NOT EXISTS `pesan` (
`nomor` int(10) NOT NULL AUTO_INCREMENT,
`idtransaksi` varchar(500) NOT NULL,
`idproduk` int(10) NOT NULL,
`jumlah` int(10) NOT NULL,
`harga` double NOT NULL,
PRIMARY KEY (`nomor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
-
Tambahkan Produk anda Pada tabel Barang
2. Membuat Config.php / api database
PHP (php/Config.php) \\Dalam folder php
<?php
mysql_connect('localhost','root','');
mysql_select_db('blog');
?>
3. Membuat Halaman Home Page / List Barang
PHP (index.php)
<?php
session_start();
if(!isset($_SESSION['cart'])){
$idt = date("ymdHis");
$_SESSION['cart'] = $idt;
}
include 'php/config.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Cart</title>
</head>
<body>
<?php
$sql='select * from barang';
$query=mysql_query($sql);
echo '<table><tr><td><strong>No</strong></td><td><strong>Nama Barang</strong></td><td><strong>Harga</strong></td><td><strong>Stok</strong></td><td><strong>Gambar</strong></td><td>Pesan</td></tr>';
$i=1;
while($rows=mysql_fetch_array($query)){
echo '<tr>
<td>'.$i.'</td>
<td>'.$rows['namabarang'].'</td>
<td>'.$rows['harga'].'</td>
<td>'.$rows['stok'].'</td>
<td><img src="php/gambar.php?id='.$rows['id'].'" width="50"></td>
<td><a href="php/pesan.php?id='.$rows['id'].'">Add to cart</a></td>
</tr>';
$i++;
}
echo '</table>';
?>
</body>
</html>
4. Membuat Halaman Cart
PHP (php/cart.php) \\dalam folder php
<?php
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>
var ajaxku;
var idnya;
function update(id){
idnya = id;
ajaxku = buatajax();
idinput = "jumlah"+id;
idloading = "loading"+id;
jumlah = document.getElementById(idinput).value;
var url="update.php";
url=url+"?id="+id;
url=url+"&jml="+jumlah
url=url+"&sid="+Math.random();
ajaxku.onreadystatechange=stateChanged;
ajaxku.open("GET",url,true);
ajaxku.send(null);
document.getElementById(idloading).innerHTML = "<img src=ajax-loader.gif>";
}
function buatajax(){
if (window.XMLHttpRequest){
return new XMLHttpRequest();
}
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function stateChanged(){
var data;
if (ajaxku.readyState==4){
data=ajaxku.responseText;
subtotalx = document.getElementById("subtotal").innerHTML;
sub = parseFloat(subtotalx);
idharga = "harga"+idnya;
idloading = "loading"+idnya;
harganya = document.getElementById(idharga).innerHTML;
hrg = parseFloat(harganya);
if(data.length>0){
hargabaru = parseFloat(data);
subtotalbaru = sub-hrg+hargabaru;
document.getElementById(idloading).innerHTML = "";
document.getElementById(idharga).innerHTML = data
document.getElementById("subtotal").innerHTML = subtotalbaru;
}
}
}
function update_cart(){
document.form1.command.value='update';
document.form1.submit();
}
</script>
<body><table border="0" cellpadding="4" cellspacing="0" width="100%">
<tr>
<td></td>
<td><b>No</b></td>
<td><b>Barang</b></td>
<td><b>Jumlah</b></td>
<td><b>Total</b></td>
<td><b>Action</b></td>
</tr>
<?php
include 'config.php';
$idtransaksi = $_SESSION['cart'];
$keranjang = mysql_query("select pesan.*, barang.namabarang, barang.id, barang.harga, pesan.nomor from pesan, barang
where pesan.idtransaksi='".$idtransaksi."' and pesan.idproduk=barang.id");
$subtotal = 0;
while($k = mysql_fetch_array($keranjang)){
echo "<tr><td><img src='gambar.php?id=".$k['id']."' width=50 height=50></td>
<td>".$k['idproduk']."</td><td class=keranjang>".$k['namabarang']."</td>";
echo "<td><input type=text size=1 value='".$k['jumlah']."' id=jumlah".$k['nomor'].">
<a href=\"javascript:update(".$k['nomor'].")\">Update</a> <span id=\"loading".$k['nomor']."\"></span></td>
<td class=keranjang>Rp. <span id=\"harga".$k['nomor']."\">".$k['harga']."</span> </td>
<td><a href='delete.php?no=".$k['nomor']."'>Delete</a></td></tr>";
$subtotal = $subtotal + $k['harga'];
}
echo "<tr><td colspan=4 align=right><b>Total</b> </td>
<td><b>Rp. <span id=subtotal>$subtotal</span></b></td></tr>";
?>
</table><br />
<a href="../">Terus Belanja</a>
</body>
</html>
5.
Membuat Program Untuk Pemesanan
PHP (php/pesan.php) \\dalam folder php
<?php
session_start();
if(!isset($_SESSION['cart'])){
$idt = date("ymdHis");
$_SESSION['cart'] = $idt;
}
include 'config.php';
$kode=$_GET['id'];
$barang = mysql_query("select * from tabelbarang where id=$kode");
$barangsql = mysql_fetch_array($barang);
$harga = $barangsql['harga'];
$sql='insert into pesan values(null,"'.$_SESSION['cart'].'","'.$kode.'",1,"'.$harga.'")';
$query=mysql_query($sql);
if($query){
header ("location:cart.php");
}
else{
echo 'gagal, Periksa database';}
?>
6. Menampilkan Gambar Dengan file gambar.php
PHP (php/Gambar.php) \\ Dalam Folder PHP
<?php
include "config.php";
$idFile = $_GET['id'];
$dataGambar = mysql_fetch_array(mysql_query("select * from barang where id='$idFile'"));
$filedata = $dataGambar['gambar'];
header("content-length: ".strlen($filedata));
echo ($filedata);
?>
7. Membuat Program Update Jumlah
PHP (php/update.php) \\dalam folder PHP
<?php
include "config.php";
$id = $_GET['id'];
$jml = $_GET['jml'];
$harga = mysql_query("select barang.harga from barang, pesan
where barang.id=pesan.idproduk and pesan.nomor=$id");
$h = mysql_fetch_array($harga);
$harganya = $h['harga'];
$hargabaru = $harganya*$jml;
$update = mysql_query("update pesan set jumlah=$jml, harga=$hargabaru where nomor=$id");
if($update){
$ambil = mysql_query("select harga from pesan where nomor=$id");
$a = mysql_fetch_array($ambil);
echo $a['harga'];
}else{
echo "error";
}
?>
8. Membuat Program Delete Pesanan
PHP (php/delete.php) \\ dalam folder PHP
<?php
include 'config.php';
$no=$_GET['no'];
$sql='delete from pesan where nomor='.$no.'';
$query=mysql_query($sql);
if($query){
header ("location:cart.php");
}
else{
echo 'gagal';}
?>