forked from muriuki92/cestas-trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
219 lines (172 loc) · 7.94 KB
/
cart.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php include('header.php') ?>
<?php include('dbcon.php') ?>
<?php
session_start();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--header section starts-->
<header>
<div class="header-1">
<a href="#" style="" class="logo"><i class="fa-solid fa-basket-shopping"></i></i>Cesta- Cart</a>
<form action="" class="search-box-container">
</form>
</div>
<!--header 2-->
<div class="header-2 " style="width: 1360px;">
<div id="menu-bar" class="fa-sharp fa-solid fa-bars"></div>
<nav class="navbar">
<a href="index.php" style="" class="nav-link active">Home</a>
<a href="#category" style="" class="nav-link active">Category</a>
<a href="#product" style="" class="nav-link active">Product</a>
<a href="contactus.php" style="" class="nav-link active">Contact</a>
<a href="admin.php" style="" class="nav-link active">Admin</a>
</nav>
<div class="icons">
<a href="cart.php" class="fa-sharp fa-solid fa-cart-shopping">
<?php
$countSql = "SELECT COUNT(*) AS total_items FROM cart";
$results = mysqli_query($dbconn, $countSql);
$rows = mysqli_fetch_assoc($results);
$totalItems = $rows['total_items'];
echo '<span style="color: #2c2c54; font-size: 15px; margin-top: 50px; border-radius: 50%;">' . $totalItems . '</span>';
?>
</a>
<?php
if (isset($_SESSION['loggedIn'])) {
} else {
echo '<a href="#.php" class="fa-sharp fa-solid fa-circle-user"></a>';
}
?>
</div>
</div>
</header>
<!--header section ends-->
<link rel="stylesheet" href="style.css">
<section class="product" id="product">
<!--header section starts-->
<!--header section ends-->
<h1 class="heading">Added To <span>Cart</span></h1>
<div class="box-container">
<?php
// Assuming you have a database connection established
include 'dbcon.php';
// Check if the add to cart button is clicked
if (isset($_POST['add_to_cart'])) {
$productName = $_POST['product_name'];
$productQuantity = $_POST['product_quantity'];
$productPrice = $_POST['product_price'];
$productImage = $_POST['product_image'];
// Insert the product details into the cart table
$insertSql = "INSERT INTO cart (name, quantity, price, image) VALUES ('$productName', '$productQuantity', '$productPrice', '$productImage')";
mysqli_query($dbconn, $insertSql);
// Provide feedback to the user
echo "<script>alert('Product added to cart.')</script>";
}
// Check if the remove from cart button is clicked
if (isset($_POST['remove_from_cart'])) {
$productName = $_POST['product_name'];
// Remove the product from the cart table
$removeSql = "DELETE FROM cart WHERE name = '$productName'";
mysqli_query($dbconn, $removeSql);
// Provide feedback to the user
echo "<script>alert('Order Succesfull')</script>";
}
// Assuming you have a database connection established
include 'dbcon.php';
// Check if the add to cart button is clicked
if (isset($_POST['add_to_cart'])) {
$productName = $_POST['product_name'];
$productQuantity = $_POST['product_quantity'];
$productPrice = $_POST['product_price'];
$productImage = $_POST['product_image'];
// Insert the product details into the cart table
$insertSql = "INSERT INTO cart (name, quantity, price, image) VALUES ('$productName', '$productQuantity', '$productPrice', '$productImage')";
mysqli_query($dbconn, $insertSql);
// Provide feedback to the user
echo "<script>alert('Product added to cart.')</script>";
}
// Check if the remove from cart button is clicked
if (isset($_POST['remove_from_cart'])) {
$productName = $_POST['product_name'];
// Remove the product from the cart table
$removeSql = "DELETE FROM cart WHERE name = '$productName'";
mysqli_query($dbconn, $removeSql);
// Provide feedback to the user
echo "<script>alert('Order Successful')</script>";
}
// Query to retrieve products from the database
$sql = "SELECT * FROM cart";
$result = mysqli_query($dbconn, $sql);
// Check if there are any products
if (mysqli_num_rows($result) > 0) {
$totalPrice = 0; // Variable to store the total price
while ($row = mysqli_fetch_assoc($result)) {
$productName = $row['name'];
$productImage = $row['image'];
$productPrice = $row['price'];
$productQuantity = $row['quantity'];
$_SESSION['name'] = $productName;
$_SESSION['price'] = $productPrice;
$_SESSION['quantity'] = $productQuantity;
$_SESSION['image'] = $productImage;
// Increment the total price
$totalPrice += $productPrice * $productQuantity;
$productTotalPrice = $productPrice * $productQuantity;
$_SESSION['total_price'] = $totalPrice;
// Generate HTML code for each product
echo '<div class="box">
<span class="discount">-30%</span>
<img src="images/' . $productImage . '" alt="">
<h3>' . $productName . '</h3>
<div class="price">Ksh <span style="text-decoration: none; color: black; font-size: 2rem;" id="productPrice_' . $productName . '">' . $productTotalPrice . '</span></div>
<form method="POST" action="./mpesa/index.php">
<input type="hidden" name="product_name" value="' . $productName . '">
<input type="hidden" name="product_price" id="productPriceInput_' . $productName . '" value="' . $productPrice * $productQuantity . '">
<input type="hidden" name="product_image" value="' . $productImage . '">
<div class="quantity">
<span>Quantity:</span>
<input type="number" min="1" max="1000" name="product_quantity" id="productQuantityInput_' . $productName . '" value="' . $productQuantity . '" onchange="updateProductPrice(\'' . $productName . '\')">
<span>/kg</span>
</div>
<div class="button-container">
<button type="submit" style="width: display: block; margin: 0 auto;" name="remove_from_cart" class="btn">Buy Now</button>
</div>
</form>
</div>';
}
// Display total price and "Buy Now" button
echo '<form method="POST" action="./mpesa/index.php">';
echo '<input type="hidden" name="total_price" value="' . $totalPrice . '">';
echo '<button type="submit" name="buy_now" class="btn">Total: ' . $totalPrice . ' Buy All</button>';
echo '</form>';
} else {
echo "<script>alert('No More products Found.')</script>";
}
?>
</div>
<script>
function updateProductPrice(productName) {
// Get the quantity and product price elements
var quantityInput = document.getElementById("productQuantityInput_" + productName);
var priceElement = document.getElementById("productPrice_" + productName);
var priceInput = document.getElementById("productPriceInput_" + productName);
// Get the updated quantity
var quantity = parseInt(quantityInput.value);
// Calculate the new product price
var productPrice = <?php echo $productPrice; ?>;
var newProductPrice = productPrice * quantity;
// Update the price elements
priceElement.innerText = newProductPrice;
priceInput.value = newProductPrice;
}
</script>
</section>
<script src="js/script.js"></script>
<script src="bootstrap-5.3.0-alpha1-dist/bootstrap.min.js"></script>