-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchScript.php
290 lines (213 loc) · 12.3 KB
/
searchScript.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
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cheapbooks";
$sql = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if(isset($_POST['id']) && isset($_POST['searchString'])) {
$id = $_POST['id'];
$searchString = $_POST['searchString'];
if($id === "searchByAuthor") {
if($searchString == "") {
$sql = "select * from book";
} else {
$sql = "select * from book where ISBN IN (select ISBN from author JOIN writtenby ON author.ssn= writtenby.ssn where name LIKE '%$searchString%')";
}
} else {
if($searchString == "") {
$sql = "select * from book";
} else {
$sql = "select * from book where title LIKE '%$searchString%'";
}
}
$result = $conn->query($sql);
$count = $result->num_rows;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$isbn = $row['ISBN'];
$sqlCheckStock = "select SUM(number) as sum from stocks where ISBN='$isbn'";
$resultCheckStock = $conn->query($sqlCheckStock);
$resultOFStocks = $resultCheckStock->fetch_assoc();
if($resultOFStocks['sum']>0) {
echo '<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<a href="#">
<img src="images/books/'.$row["ISBN"].'.jpg" class="img-thumbnail" />
</a>
<div class="caption text-center">
<h6><a href="#" >'.$row['title'].', '.$row['year'].'</a></h6>
<h5> Items in stock: '.$resultOFStocks['sum'].'</h5>
<h5> ISBN: '.$row["ISBN"].'</h5>
<button class="btn btn-primary btn-xs addToCart" data-ISBN="'.$row["ISBN"].'" data-totalItems= '.$resultOFStocks['sum'].'"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</button></span>
<label for="movie"></label><input style="width:60px;" id="number_'.$row["ISBN"].'" type="number" value="1" min="1" max="'.$resultOFStocks['sum'].'"/>
</div>
</div>
</div>';
} else {
$count = $count - 1;
}
}
if($count == 0) {
echo "<br/><div class='alert alert-danger'>The books searched for are not in stock</div>";
}
} else {
echo "<br/><div class='alert alert-danger'>The books searched for are not in stock</div>";
}
}
else if(isset($_POST['action']) && $_POST['action'] === "addToCart"){
// $action = $_POST['action'];
$isbn = $_POST['isbn'];
$number = $_POST['number'];
$total_stock = $_POST['totalStock'];
addToCart($isbn,$number,$total_stock,$conn);
} else if(isset($_POST['action']) && $_POST['action'] === "buyBooks") {
buyBooks($conn);
}
function addToCart($isbn,$number,$total_stock,$conn) {
$basketID = $_SESSION["basketID"];
//
// $sql = "INSERT INTO contains (ISBN, basketID, number)
//VALUES ('$isbn', '$basketID','$number')
//ON DUPLICATE KEY UPDATE
//ISBN='$isbn', basketID='$basketID', number= number + '$number'";
// $resultSql = $conn->query($sql);
//
// if($resultSql) {
//
// echo "Added to cart succeffully";
// } else {
//
// echo "error";
// }
$sqlCheckIfExists = "select * from contains where ISBN = '$isbn' and basketID = '$basketID'";
$resultSql = $conn->query($sqlCheckIfExists);
if ($resultSql->num_rows > 0) {
// output data of each row
while($row = $resultSql->fetch_assoc()) {
if($number < ( $total_stock - $row['number'])){
$updateQuery = "update contains set number = number + '$number' where ISBN = '$isbn' and basketID = '$basketID'";
if($conn->query($updateQuery)) {
echo "Added to cart Succesfully";
$getBasketdetails = "select * from book JOIN contains ON book.ISBN=contains.ISBN where contains.ISBN IN (select ISBN from contains JOIN shoppingbasket ON contains.basketID = shoppingbasket.basketID where shoppingbasket.basketID = '$basketID') and contains.basketID = '$basketID'";
$resultOfCart = ($conn->query($getBasketdetails));
$_SESSION["totalBasketItems"] = $resultOfCart->num_rows;
$_SESSION['cartItems'] = array();
if($resultOfCart->num_rows > 0) {
while($basketResult = $resultOfCart->fetch_assoc()) {
$_SESSION['cartItems'][]= $basketResult;
}
}
} else {
echo "There was an issue";
}
} else if($number > ( $total_stock - $row['number']) && $total_stock - $row['number'] != 0){
$number = $total_stock - $row['number'];
$updateQuery = "update contains set number = number + '$number' where ISBN = '$isbn' and basketID = '$basketID'";
if($conn->query($updateQuery)) {
echo "Added to cart Succesfully";
$getBasketdetails = "select * from book JOIN contains ON book.ISBN=contains.ISBN where contains.ISBN IN (select ISBN from contains JOIN shoppingbasket ON contains.basketID = shoppingbasket.basketID where shoppingbasket.basketID = '$basketID') and contains.basketID = '$basketID'";
$resultOfCart = ($conn->query($getBasketdetails));
$_SESSION["totalBasketItems"] = $resultOfCart->num_rows;
$_SESSION['cartItems'] = array();
if($resultOfCart->num_rows > 0) {
while($basketResult = $resultOfCart->fetch_assoc()) {
$_SESSION['cartItems'][]= $basketResult;
}
}
} else {
echo "There was an issue in adding item to cart";
}
} else {
echo "Cart already contains the total items in the stock. Cannot add more item.Select the items in stock.";
}
}
} else {
$sql= "insert into contains values('$isbn','$basketID','$number')";
$result = $conn->query($sql);
if ($result) {
echo "Added to cart Successfully.";
$_SESSION['cartItems'] = array();
$getBasketdetails = "select * from book JOIN contains ON book.ISBN=contains.ISBN where contains.ISBN IN (select ISBN from contains JOIN shoppingbasket ON contains.basketID = shoppingbasket.basketID where shoppingbasket.basketID = '$basketID') and contains.basketID = '$basketID'";
$resultOfCart = ($conn->query($getBasketdetails));
$_SESSION['cartItems'] = array();
$_SESSION["totalBasketItems"] = $resultOfCart->num_rows;
if($resultOfCart->num_rows > 0) {
while($basketResult = $resultOfCart->fetch_assoc()) {
$_SESSION['cartItems'][]= $basketResult;
}
}
} else {
echo "There was an issue in adding item to cart";
}
}
}
function buyBooks($conn) {
$basketID = $_SESSION["basketID"];
$username = $_SESSION["username"];
$getBasketdetails = "select * from contains where basketID = '$basketID'";
$resultOfCart = ($conn->query($getBasketdetails));
while($cartDetails = $resultOfCart->fetch_assoc()) {
$ISBN = $cartDetails['ISBN'];
$cartItemsNumber = $cartDetails['number'];
$getQueryForStocks = "select * from stocks where ISBN = '$ISBN' and number > 0";
$resultOfQueryForStocks = ($conn->query($getQueryForStocks));
if($resultOfQueryForStocks->num_rows > 0) {
while($stockDetails = $resultOfQueryForStocks->fetch_assoc()) {
$wareHouseCode = $stockDetails['warehouseCode'];
if($cartItemsNumber > 0) {
if($cartItemsNumber <= $stockDetails['number']) {
//insert normal way
//decrement from stcks
$insertIntoShiping = "insert into shippingorder values('$ISBN','$wareHouseCode','$username','$cartItemsNumber')";
if($conn->query($insertIntoShiping)){
$updateStockTable = "update stocks set `number` = `number` - $cartItemsNumber where ISBN = '$ISBN' and warehouseCode = '$wareHouseCode'";
if($conn->query($updateStockTable)) {
echo "successfully updated stocks table";
} else {
echo "There was an error updating stocks table";
}
} else {
echo "successfully inserted into shipppng table.";
}
} else if($stockDetails['number'] != 0 && $cartItemsNumber>$stockDetails['number']) {
//insert with stock number
$cartItemsNumber = $cartItemsNumber -($cartItemsNumber - $stockDetails['number']);
$insertIntoShiping = "insert into shippingorder values('$ISBN','$wareHouseCode','$username','$cartItemsNumber')";
if($conn->query($insertIntoShiping)){
$updateStockTable = "update stocks set number = number - '$cartItemsNumber' where ISBN = '$ISBN' and warehouseCode = '$wareHouseCode'";
if($conn->query($updateStockTable)) {
echo "successfully updated stocks table";
} else {
echo "There was an error updating stocks table";
}
} else {
echo "suceesffully inserted into shipppng table";
}
} else {
// echo "here in break";
break;
}
} else {
echo "no more items in the cart";
break;
}
}
//$_SESSION['totalBasketItems']
$deleteFromContain = "delete from contains where ISBN = '$ISBN' and basketID = '$basketID'";
if($conn->query($deleteFromContain)) {
$_SESSION["cartItems"] = array();
$_SESSION['totalBasketItems'] = 0;
echo "items are deleted from the cart";
} else {
echo "could not delete ";
}
} else {
//no stock available
echo "items are no longer available.";
}
}
}
?>