-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
367 lines (327 loc) · 12.2 KB
/
action.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
session_start();
$ip_add = getenv("REMOTE_ADDR");
include "db.php";
if(isset($_POST["category"])){
$category_query = "SELECT * FROM categories";
$run_query = mysqli_query($con,$category_query) or die(mysqli_error($con));
echo "
<div class='nav nav-pills nav-stacked'>
<li class='active'><a href='#'><h4>Product Categories</h4></a></li>
";
if(mysqli_num_rows($run_query) > 0){
while($row = mysqli_fetch_array($run_query)){
$cid = $row["cat_id"];
$cat_name = $row["cat_title"];
echo "
<li><a href='#' class='category' cid='$cid'>$cat_name</a></li>
";
}
echo "</div>";
}
}
if(isset($_POST["brand"])){
$brand_query = "SELECT * FROM brands";
$run_query = mysqli_query($con,$brand_query);
echo "
<div class='nav nav-pills nav-stacked'>
<li class='active'><a href='#'><h4>Brands</h4></a></li>
";
if(mysqli_num_rows($run_query) > 0){
while($row = mysqli_fetch_array($run_query)){
$bid = $row["brand_id"];
$brand_name = $row["brand_title"];
echo "
<li><a href='#' class='selectBrand' bid='$bid'>$brand_name</a></li>
";
}
echo "</div>";
}
}
if(isset($_POST["page"])){
$sql = "SELECT * FROM products";
$run_query = mysqli_query($con,$sql);
$count = mysqli_num_rows($run_query);
$pageno = ceil($count/9);
for($i=1;$i<=$pageno;$i++){
echo "
<li><a href='#' page='$i' id='page'>$i</a></li>
";
}
}
if(isset($_POST["getProduct"])){
$limit = 9;
if(isset($_POST["setPage"])){
$pageno = $_POST["pageNumber"];
$start = ($pageno * $limit) - $limit;
}else{
$start = 0;
}
$product_query = "SELECT * FROM products LIMIT $start,$limit";
$run_query = mysqli_query($con,$product_query);
if(mysqli_num_rows($run_query) > 0){
while($row = mysqli_fetch_array($run_query)){
$pro_id = $row['product_id'];
$pro_cat = $row['product_cat'];
$pro_brand = $row['product_brand'];
$pro_title = $row['product_title'];
$pro_price = $row['product_price'];
$pro_image = $row['product_image'];
echo "
<div class='col-md-4'>
<div class='panel panel-info'>
<div class='panel-heading'>$pro_title</div>
<div class='panel-body'>
<img src='product_images/$pro_image' style='width:220px; height:250px;'/>
</div>
<div class='panel-heading'>".CURRENCY.". $pro_price.00/-
<button pid='$pro_id' style='float:right;' id='product' class='btn btn-danger btn-xs'>Add To Cart</button>
</div>
</div>
</div>
";
}
}
}
if(isset($_POST["get_seleted_Category"]) || isset($_POST["selectBrand"]) || isset($_POST["search"])){
if(isset($_POST["get_seleted_Category"])){
$id = $_POST["cat_id"];
$sql = "SELECT * FROM products WHERE product_cat = '$id'";
}else if(isset($_POST["selectBrand"])){
$id = $_POST["brand_id"];
$sql = "SELECT * FROM products WHERE product_brand = '$id'";
}else {
$keyword = $_POST["keyword"];
$sql = "SELECT * FROM products WHERE product_keywords LIKE '%$keyword%'";
}
$run_query = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($run_query)){
$pro_id = $row['product_id'];
$pro_cat = $row['product_cat'];
$pro_brand = $row['product_brand'];
$pro_title = $row['product_title'];
$pro_price = $row['product_price'];
$pro_image = $row['product_image'];
echo "
<div class='col-md-4'>
<div class='panel panel-info'>
<div class='panel-heading'>$pro_title</div>
<div class='panel-body'>
<img src='product_images/$pro_image' style='width:220px; height:250px;'/>
</div>
<div class='panel-heading'>Rs.$pro_price.00/-
<button pid='$pro_id' style='float:right;' id='product' class='btn btn-danger btn-xs'>Add To Cart</button>
</div>
</div>
</div>
";
}
}
if(isset($_POST["addToCart"])){
$p_id = $_POST["proId"];
if(isset($_SESSION["uid"])){
$user_id = $_SESSION["uid"];
$sql = "SELECT * FROM cart WHERE p_id = '$p_id' AND user_id = '$user_id'";
$run_query = mysqli_query($con,$sql);
$count = mysqli_num_rows($run_query);
if($count > 0){
echo "
<div class='alert alert-warning'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is already added into the cart Continue Shopping..!</b>
</div>
";//not in video
} else {
$sql = "INSERT INTO `cart`
(`p_id`, `ip_add`, `user_id`, `qty`)
VALUES ('$p_id','$ip_add','$user_id','1')";
if(mysqli_query($con,$sql)){
echo "
<div class='alert alert-success'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is Added..!</b>
</div>
";
}
}
}else{
$sql = "SELECT id FROM cart WHERE ip_add = '$ip_add' AND p_id = '$p_id' AND user_id = -1";
$query = mysqli_query($con,$sql);
if (mysqli_num_rows($query) > 0) {
echo "
<div class='alert alert-warning'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is already added into the cart Continue Shopping..!</b>
</div>";
exit();
}
$sql = "INSERT INTO `cart`
(`p_id`, `ip_add`, `user_id`, `qty`)
VALUES ('$p_id','$ip_add','-1','1')";
if (mysqli_query($con,$sql)) {
echo "
<div class='alert alert-success'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Your product has been added to cart!</b>
</div>
";
exit();
}
}
}
//Count User cart item
if (isset($_POST["count_item"])) {
//When user is logged in then we will count number of item in cart by using user session id
if (isset($_SESSION["uid"])) {
$sql = "SELECT COUNT(*) AS count_item FROM cart WHERE user_id = $_SESSION[uid]";
}else{
//When user is not logged in then we will count number of item in cart by using users unique ip address
$sql = "SELECT COUNT(*) AS count_item FROM cart WHERE ip_add = '$ip_add' AND user_id < 0";
}
$query = mysqli_query($con,$sql);
$row = mysqli_fetch_array($query);
echo $row["count_item"];
exit();
}
//Count User cart item
//Get Cart Item From Database to Dropdown menu
if (isset($_POST["Common"])) {
if (isset($_SESSION["uid"])) {
//When user is logged in this query will execute
$sql = "SELECT a.product_id,a.product_title,a.product_price,a.product_image,b.id,b.qty FROM products a,cart b WHERE a.product_id=b.p_id AND b.user_id='$_SESSION[uid]'";
}else{
//When user is not logged in this query will execute
$sql = "SELECT a.product_id,a.product_title,a.product_price,a.product_image,b.id,b.qty FROM products a,cart b WHERE a.product_id=b.p_id AND b.ip_add='$ip_add' AND b.user_id < 0";
}
$query = mysqli_query($con,$sql);
if (isset($_POST["getCartItem"])) {
//display cart item in dropdown menu
if (mysqli_num_rows($query) > 0) {
$n=0;
while ($row=mysqli_fetch_array($query)) {
$n++;
$product_id = $row["product_id"];
$product_title = $row["product_title"];
$product_price = $row["product_price"];
$product_image = $row["product_image"];
$cart_item_id = $row["id"];
$qty = $row["qty"];
echo '
<div class="row">
<div class="col-md-3">'.$n.'</div>
<div class="col-md-3"><img class="img-responsive" src="product_images/'.$product_image.'" /></div>
<div class="col-md-3">'.$product_title.'</div>
<div class="col-md-3">'.CURRENCY.''.$product_price.'</div>
</div>';
}
?>
<a style="float:right;" href="cart.php" class="btn btn-warning">Edit <span class="glyphicon glyphicon-edit"></span></a>
<?php
exit();
}
}
if (isset($_POST["checkOutDetails"])) {
if (mysqli_num_rows($query) > 0) {
//display user cart item with "Ready to checkout" button if user is not login
echo "<form method='post' action='login_form.php'>";
$n=0;
while ($row=mysqli_fetch_array($query)) {
$n++;
$product_id = $row["product_id"];
$product_title = $row["product_title"];
$product_price = $row["product_price"];
$product_image = $row["product_image"];
$cart_item_id = $row["id"];
$qty = $row["qty"];
echo
'<div class="row">
<div class="col-md-2">
<div class="btn-group">
<a href="#" remove_id="'.$product_id.'" class="btn btn-danger remove"><span class="glyphicon glyphicon-trash"></span></a>
<a href="#" update_id="'.$product_id.'" class="btn btn-primary update"><span class="glyphicon glyphicon-ok-sign"></span></a>
</div>
</div>
<input type="hidden" name="product_id[]" value="'.$product_id.'"/>
<input type="hidden" name="" value="'.$cart_item_id.'"/>
<div class="col-md-2"><img class="img-responsive" src="product_images/'.$product_image.'"></div>
<div class="col-md-2">'.$product_title.'</div>
<div class="col-md-2"><input type="text" class="form-control qty" value="'.$qty.'" ></div>
<div class="col-md-2"><input type="text" class="form-control price" value="'.$product_price.'" readonly="readonly"></div>
<div class="col-md-2"><input type="text" class="form-control total" value="'.$product_price.'" readonly="readonly"></div>
</div>';
}
echo '<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4">
<b class="net_total" style="font-size:20px;"> </b>
</div>';
if (!isset($_SESSION["uid"])) {
echo '<input type="submit" style="float:right;" name="login_user_with_product" class="btn btn-info btn-lg" value="Ready to Checkout" >
</form>';
}else if(isset($_SESSION["uid"])){
//Paypal checkout form
echo '
</form>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="upload" value="1">';
$x=0;
$sql = "SELECT a.product_id,a.product_title,a.product_price,a.product_image,b.id,b.qty FROM products a,cart b WHERE a.product_id=b.p_id AND b.user_id='$_SESSION[uid]'";
$query = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($query)){
$x++;
echo
'<input type="hidden" name="item_name_'.$x.'" value="'.$row["product_title"].'">
<input type="hidden" name="item_number_'.$x.'" value="'.$x.'">
<input type="hidden" name="amount_'.$x.'" value="'.$row["product_price"].'">
<input type="hidden" name="quantity_'.$x.'" value="'.$row["qty"].'">';
}
echo
'<input type="hidden" name="return" value="http://localhost/project1/payment_success.php"/>
<input type="hidden" name="notify_url" value="http://localhost/ecommerce-app-h/payment_success.php">
<input type="hidden" name="cancel_return" value="http://localhost/ecommerce-app-h/cancel.php"/>
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="custom" value="'.$_SESSION["uid"].'"/>
<input style="float:right;margin-right:80px;" type="image" name="submit"
src="https://www.paypalobjects.com/webstatic/en_US/i/btn/png/blue-rect-paypalcheckout-60px.png" alt="PayPal Checkout"
alt="PayPal - The safer, easier way to pay online">
</form>';
}
}
}
}
//Remove Item From cart
if (isset($_POST["removeItemFromCart"])) {
$remove_id = $_POST["rid"];
if (isset($_SESSION["uid"])) {
$sql = "DELETE FROM cart WHERE p_id = '$remove_id' AND user_id = '$_SESSION[uid]'";
}else{
$sql = "DELETE FROM cart WHERE p_id = '$remove_id' AND ip_add = '$ip_add'";
}
if(mysqli_query($con,$sql)){
echo "<div class='alert alert-danger'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is removed from cart</b>
</div>";
exit();
}
}
//Update Item From cart
if (isset($_POST["updateCartItem"])) {
$update_id = $_POST["update_id"];
$qty = $_POST["qty"];
if (isset($_SESSION["uid"])) {
$sql = "UPDATE cart SET qty='$qty' WHERE p_id = '$update_id' AND user_id = '$_SESSION[uid]'";
}else{
$sql = "UPDATE cart SET qty='$qty' WHERE p_id = '$update_id' AND ip_add = '$ip_add'";
}
if(mysqli_query($con,$sql)){
echo "<div class='alert alert-info'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is updated</b>
</div>";
exit();
}
}
?>