Advertisement
safriansah

keranjang class

Jul 8th, 2019
1,491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2. class Keranjang{
  3.     var $koneksi;
  4.     var $base_url;
  5.     var $table='tb_keranjang';
  6.  
  7.     function __construct($koneksi, $base_url){
  8.         $this->koneksi=$koneksi;
  9.         $this->base_url=$base_url;
  10.     }
  11.    
  12.     function getAllData(){
  13.         $no = 0;
  14.         $id_user=$_SESSION['id'];
  15.         $data = mysqli_query($this->koneksi,"select * from $this->table where id_user='$id_user' order by id asc");
  16.         while($d = mysqli_fetch_array($data)){
  17.             $result[$no]=$d;
  18.             $no++;
  19.         }
  20.         if($no<1) return null;
  21.         else return $result;
  22.     }
  23.  
  24.     function getKeranjangCount(){
  25.         $id_user=$_SESSION['id'];
  26.         $data=mysqli_query($this->koneksi,"select * from $this->table where id_user='$id_user'");
  27.         $res=mysqli_num_rows($data);
  28.         return $res;
  29.     }
  30.    
  31.     function addKeranjang(){
  32.         $qty=$_POST['qty'];
  33.         if($qty<1){
  34.             echo("<script LANGUAGE='JavaScript'>
  35.                window.alert('Qty tidak valid');
  36.                window.location.href='".$this->base_url."index.php';
  37.            </script>");
  38.             return;
  39.         }
  40.         $id_user=$_SESSION['id'];
  41.         $id_barang=$_POST['id'];
  42.         $data=mysqli_query($this->koneksi,"select * from $this->table where id_user='$id_user' and id_barang='$id_barang'");
  43.         $res=mysqli_num_rows($data);
  44.         if($res<1) $query="insert into $this->table(id_user, id_barang, jumlah) values('$id_user', '$id_barang', '$qty')";
  45.         else $query="update $this->table set jumlah=jumlah+$qty where id_user='$id_user' and id_barang='$id_barang'";
  46.         $res=mysqli_query($this->koneksi, $query);
  47.         if($res)
  48.         echo("<script LANGUAGE='JavaScript'>
  49.                window.alert('Berhasil ditambahkan');
  50.                window.location.href='".$this->base_url."index.php';
  51.            </script>");
  52.         else
  53.         echo("<script LANGUAGE='JavaScript'>
  54.                window.alert('Gagal ditambahkan');
  55.                window.location.href='".$this->base_url."index.php';
  56.            </script>");
  57.     }
  58.  
  59.     function delKeranjang(){
  60.         $id=$_POST['id'];
  61.         $query="delete from $this->table where id='$id'";
  62.         $res=mysqli_query($this->koneksi, $query);
  63.         if($res)
  64.         echo("<script LANGUAGE='JavaScript'>
  65.                window.alert('Berhasil dihapus');
  66.                window.location.href='".$this->base_url."cart.php';
  67.            </script>");
  68.         else
  69.         echo("<script LANGUAGE='JavaScript'>
  70.                window.alert('Gagal dihapus');
  71.                window.location.href='".$this->base_url."cart.php';
  72.            </script>");
  73.     }
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement