-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshop.php
90 lines (81 loc) · 2.24 KB
/
shop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
session_start();
include('db.php');
$status="";
if (isset($_POST['code']) && $_POST['code']!=""){
$code = $_POST['code'];
$result = mysqli_query($con,"SELECT * FROM `products` WHERE `code`='$code'");
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$code = $row['code'];
$price = $row['price'];
$image = $row['image'];
$cartArray = array(
$code=>array(
'name'=>$name,
'code'=>$code,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($code,$array_keys)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge($_SESSION["shopping_cart"],$cartArray);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
<html>
<head>
<title>Shop</title>
<!-- Favicon -->
<link rel="icon" href="favicon.png" type="image/png">
<link rel='stylesheet' href='styles/shop.css' type='text/css' media='all' />
<!-- JSON -->
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- Navbar -->
<?php include 'navbar.php'; ?>
<div>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="cart-icon.png" /> Cart<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
echo "<form method='post' action=''>
<div class='product_wrapper'>
<input type='hidden' name='code' value=".$row['code']." />
<div class='image'><img class='img-style' src='".$row['image']."' /></div>
<div class='name'>".$row['name']."</div>
<div class='price'>$".$row['price']."</div>
<button type='submit' class='buy'>Buy Now</button>
</form>
</div>";
}
mysqli_close($con);
?>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
<div style="text-align: center;">
<img src="img/quickauction.png">
</div>
</div>
</body>
</html>