Advertisement
andri_ganda_saputra1

require aksi sebelum script yg tadi....

Nov 19th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2. session_start();
  3. error_reporting(0);
  4. include "config/koneksi.php";
  5. include "config/library.php";
  6.  
  7. if (empty($_SESSION[username]) AND empty($_SESSION[password])) {
  8.     header('location:home.php?module=warning');
  9. }
  10. else {
  11.  
  12.     $module=$_GET[module];
  13.     $act=$_GET[act];
  14.  
  15.     if ($module=='keranjang' AND $act=='tambah'){
  16.    
  17.         $sid = $_SESSION[email];
  18.    
  19.         $sql2 = mysqli_query($con,"SELECT stok FROM produk WHERE id_produk='$_GET[id]'");
  20.         $r=mysqli_fetch_array($sql2);
  21.         $stok=$r[stok];
  22.      
  23.       if ($stok == 0){
  24.           echo "stok habis";
  25.       }
  26.       else{
  27.         // check if the product is already
  28.         // in cart table for this session
  29.         $sql = mysqli_query($con,"SELECT id_produk FROM orders_temp
  30.                 WHERE id_produk='$_GET[id]' AND id_session='$sid'");
  31.         $ketemu=mysqli_num_rows($sql);
  32.         if ($ketemu==0){
  33.             // put the product in cart table
  34.             mysqli_query($con,"INSERT INTO orders_temp (id_produk, jumlah, id_session, tgl_order_temp, jam_order_temp, stok_temp)
  35.                     VALUES ('$_GET[id]', 1, '$sid', '$tgl_sekarang', '$jam_sekarang', '$stok')");
  36.         } else {
  37.             // update product quantity in cart table
  38.             mysqli_query($con,"UPDATE orders_temp
  39.                     SET jumlah = jumlah + 1
  40.                     WHERE id_session ='$sid' AND id_produk='$_GET[id]'");      
  41.         }  
  42.         deleteAbandonedCart();
  43.         header('Location:home.php?module=keranjangbelanja');
  44.       }            
  45.     }
  46.  
  47.     elseif ($module=='keranjang' AND $act=='hapus'){
  48.         mysqli_query($con,"DELETE FROM orders_temp WHERE id_orders_temp='$_GET[id]'");
  49.         header('Location:home.php?module=keranjangbelanja');               
  50.     }
  51.  
  52. elseif ($module=='keranjang' AND $act=='update'){
  53.   $id       = $_POST[id];
  54.   $jml_data = count($id);
  55.   $jumlah   = $_POST[jml]; // quantity
  56.   for ($i=1; $i <= $jml_data; $i++){
  57.     $sql2 = mysqli_query($con,"SELECT stok_temp FROM orders_temp    WHERE id_orders_temp='".$id[$i]."'");
  58.     while($r=mysqli_fetch_array($sql2)){
  59.     if ($jumlah[$i] > $r[stok_temp]){
  60.         echo "<script>window.alert('Jumlah yang dibeli melebihi stok yang ada');
  61.        window.location=('home.php?module=keranjangbelanja')</script>";
  62.     }
  63.     elseif($jumlah[$i] == 0){
  64.         echo "<script>window.alert('Anda tidak boleh menginputkan angka 0 atau mengkosongkannya!');
  65.        window.location=('home.php?module=keranjangbelanja')</script>";
  66.     }
  67.     else{
  68.       mysqli_query($con,"UPDATE orders_temp SET jumlah = '".$jumlah[$i]."'
  69.                                      WHERE id_orders_temp = '".$id[$i]."'");
  70.       header('Location:home.php?module=keranjangbelanja');
  71.     }
  72.   }
  73.   }
  74. }
  75. }
  76.  
  77. /*
  78.     Delete all cart entries older than one day
  79. */
  80. function deleteAbandonedCart(){
  81.     $kemarin = date('Y-m-d', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
  82.     mysqli_query($con,"DELETE FROM orders_temp
  83.             WHERE tgl_order_temp < '$kemarin'");
  84. }
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement