-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_products.php
143 lines (115 loc) · 5.08 KB
/
view_products.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
include 'components/connection.php';
session_start();
if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id'];
} else {
$user_id = '';
}
if (isset($_POST['layout'])) {
session_destroy();
header("location:login.php");
}
//Adding product in wishlist
if (isset($_POST['add_to_wishlist'])) {
$id = unique_id();
$product_id = $_POST['product_id'];
// $varify_wishlist = $conn->prepare("SELECT * FROM `wishlist` WHERE user_id = ? AND product_id =?");
// $verify_wishlist->execute([$user_id, $product_id]);
$cart_num = $conn->prepare("SELECT * FROM `cart` WHERE user_id = ? AND product_id =? ");
$cart_num->execute([$user_id, $product_id]);
// if ($varify_wishlist->rowCount() > 0) {
// $warning_msg[] = 'product already exist in your wishlist';
// } else
if ($cart_num->rowCount() > 0) {
$warning_msg[] = 'product already exist';
} else {
$select_price = $conn->prepare("SELECT * FROM `products` WHERE id=? LIMIT 1");
$select_price->execute([$product_id]);
$fetch_price = $select_price->fetch(PDO::FETCH_ASSOC);
$insert_wishlist = $conn->prepare("INSERT INTO `wishlist` (id, user_id, product_id,price) VALUES(?,?,?,?)");
$insert_wishlist->execute([$id, $user_id, $product_id, $fetch_price['price']]);
$success_msg[] = 'product added to wishlist successfully';
}
}
// adding products in cart
if (isset($_POST['add_to_cart'])) {
$id = unique_id();
$product_id = $_POST['product_id'];
$qty = $_POST['qty'];
$qty = filter_var($qty, FILTER_SANITIZE_STRING);
$varify_cart = $conn->prepare("SELECT * FROM `cart` WHERE user_id = ? AND product_id =? ");
$varify_cart->execute([$user_id, $product_id]);
$max_cart_items = $conn->prepare("SELECT * FROM `cart`WHERE user_id = ?");
$max_cart_items->execute([$user_id]);
// if ($varify_wishlist->rowCount() > 0) {
// $warning_msg[] = 'product already exist in your wishlist';
// } else
if ($max_cart_items->rowCount() > 20) {
$warning_msg[] = 'cart is full!';
} else {
$select_price = $conn->prepare("SELECT * FROM `products` WHERE id=? LIMIT 1");
$select_price->execute([$product_id]);
$fetch_price = $select_price->fetch(PDO::FETCH_ASSOC);
$insert_cart = $conn->prepare("INSERT INTO `cart` (id, user_id, product_id,price,qty) VALUES(?,?,?,?,?)");
$insert_cart->execute([$id, $user_id, $product_id, $fetch_price['price'], $qty]);
$success_msg[] = 'product added to cart successfully';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="style.css">
<title>MyWebsite - shop </title>
</head>
<body>
<?php include './components/header.php'; ?>
<div class="main">
<div class="banner">
<h1>shop</h1>
</div>
<div class="title2">
<a href="home.php">home</a><span>/ our shop</span>
</div>
<section class="products">
<div class="box-container">
<?php
$select_products = $conn->prepare("SELECT * FROM `products`");
$select_products->execute();
if ($select_products->rowCount() > 0) {
while ($fetch_products = $select_products->fetch(PDO::FETCH_ASSOC)) {
?>
<form action="" method="post" class="box">
<img src="image/<?= $fetch_products['image']; ?>" alt="">
<div class="button">
<button type="submit" name="add_to_cart"><i class="bx bx-cart"></i> </button>
<button type="submit" name="add_to_wishlist"><i class="bx bx-heart"></i> </button>
<a href="view_page.php?pid=<?php echo $fetch_products['id']; ?>" class="bx bxs-show"></a>
</div>
<h3 class="name"><?= $fetch_products['name']; ?></h3>
<input type="hidden" name="product_id" value="<?= $fetch_products['id']; ?>">
<div class="flex">
<input type="number" name="qty" required min="1" value="1" max="99" maxlength="2" class="qty">
</div>
<a href="checkout.php?get_id=<?= $fetch_products['id']; ?>" class="btn">buy now</a>
</form>
<?php
}
} else {
echo '
<p class="empty">no products added yet!</p>';
}
?>
</div>
</section>
<?php include 'components/footer.php'; ?>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script src="script.js"></script>
<?php include './components/alert.php'; ?>
</body>
</html>