-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoozeNationFunctions.php
303 lines (216 loc) · 11.5 KB
/
BoozeNationFunctions.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
<?php
/**
* this document contain PHP functions used in BoozeNation WebAPP
* Author: Ayodeji Eniabire 000878946
* Date: November22, 2022
*/
// connection with database
$servername = "localhost";
$username = "root";
$password = "mohawkcollege558";
$databaseName = "mydatabase";
$severConnection = mysqli_connect($servername, $username, $password, $databaseName);
if(!$severConnection){
die("Connection failed: " . mysqli_connect_error());
}
/**
* recieve itemlist request,
* get requested list from database,
* format list in HTML tags and
* echo HTML formated list
*/
function CategoryItems($Category, $Offset, $Limit ) {
$tableData = '<tr><th>Product ID</th><th>Product Name</th><th>Product Price</th><th>Product Category</th><th>Product Quantity</th></tr>'; // format itemlist table header
// assign post request data for variable name
$Offset = (int) $_POST["Offset"];
$Limit = (int) $_POST["Limit"];
$Category = $_POST["Category"];
GLOBAL $severConnection; // secure connection to database
// logic for database search query to used based on requested list category
if ($Category == 'All'){ // if item list in all category is requested
$sql = "SELECT product_id, product_name, product_price,product_category, product_quantity FROM catalogue ORDER BY product_name ;";
$result = mysqli_query($severConnection, $sql);
$NumberOfRow = mysqli_num_rows($result);
$sql = "SELECT product_id, product_name, product_price,product_category, product_quantity FROM catalogue ORDER BY product_name LIMIT $Offset, $Limit ;"; // Database selecte query
}
else{ // if item list in selected category is requested
$sql = "SELECT product_id, product_name, product_price,product_category, product_quantity FROM catalogue WHERE product_category= '$Category' ORDER BY product_name ;";
$result = mysqli_query($severConnection, $sql);
$NumberOfRow = mysqli_num_rows($result);
$sql = "SELECT product_id, product_name, product_price,product_category, product_quantity FROM catalogue WHERE product_category= '$Category' ORDER BY product_name LIMIT $Offset, $Limit ;"; // Database select querey
}
$result = mysqli_query($severConnection, $sql); // asign search result to a variable name
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { // assign search result to an associative array
// insert search result in HTML table formt. function quantitycheck check if item quantity is 0 and display out of stock
$tablerow = '<td id="'.$row['product_id'].'" >'. $row['product_id']. '</td>'. '<td>'. $row['product_name']. '</td>'. '<td>'. $row['product_price']. '</td>'. '<td>' .$row['product_category']. '</td>'. '<td>'. quantityCheck($row['product_quantity']). '</td>' ;
$tableData .= '<tr onclick="MakeRequest(\'' . 'item' . '\' , \'' . $row['product_id'] . '\' , \'' . 'table'. '\' )" >'. $tablerow . '</tr>'; // add onclick to each table row
}
}
echo AddButtons($NumberOfRow, $Limit, $Offset, $tableData ); // echo table with previous and next button
}
/**
* this function add next and previous button to item list using limit and offset as refrence
*/
function AddButtons($NumberOfRow, $Limit, $Offset, $tableData ){
$NextButton = '<h4 id="buttonN" onclick="LoadItem(\'' . 'Category' . '\' , \'' . 'All' . '\' , \'' . 'Limit'. '\' , \'' . $Limit. '\' , \'' . 'Offset'. '\' , \'' . $Offset + 7 . '\' , \'' . 'table'. '\' )">Next</h4>';
$PrevButton = '<h4 id="buttonP" onclick="LoadItem(\'' . 'Category' . '\' , \'' . 'All' . '\' , \'' . 'Limit'. '\' , \'' . $Limit. '\' , \'' . 'Offset'. '\' , \'' . $Offset - 7 . '\' , \'' . 'table'. '\' )">Prev</h4>';
// add only next button if list is more than 7 rows
if (($NumberOfRow > $Limit )&& ($Offset <= 0) && ($Offset < $NumberOfRow) ) {
$table = $tableData. '<tr id="button">'. '<td>'. $NextButton. '</td>'. '</tr>';
}
// add next and previous button on subsequent pages if there are more rows
if($NumberOfRow > 7 && $Offset > 0 && $Offset < $NumberOfRow){
$table = $tableData. '<tr id="button">'. '<td>'. $PrevButton. '</td>'.'<td>'. $NextButton. '</td>'. '</tr>';
}
// add only previous button to subsequent page if there are no more rows after
if($NumberOfRow > 7 && ($Offset+7) >= $NumberOfRow){
$table = $table = $tableData. '<tr id="button">'. '<td>'. $PrevButton. '</td>'. '</tr>';
}
// dont add next or previous button if list is not more than 7 rows
if ($NumberOfRow <= 7){
$table = $tableData;
}
return $table;
}
/**
* get request to view item description
* retrive item description form database
* insert item description in HTML table format
* echo item description
*/
function ItemView() {
$id = $_POST['item']; // assign post request data to variable
$tableData = '<tr><th>Product ID</th><th>Product Name</th><th>Product Description</th>'; // create table header
GLOBAL $severConnection; // secure database connection
$sql = "SELECT product_id, product_name, product_description, product_quantity FROM catalogue WHERE product_id= '$id' ;"; // database select statement
$result = mysqli_query($severConnection, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$quantity = $row['product_quantity']; // assign item quantity in database to variable
$input = $row['product_id']; // assign item id in database to a variable
$tablerow = '<td>'. $row['product_id']. '</td>'. '<td>'. $row['product_name']. '</td>'. '<td>'. $row['product_description']. '</td>'. '<td>'. CartCheck($quantity, $input); // this function check for product quantiy and contol its addition to cart
$tableData .= '<tr>'. $tablerow . '</tr>' ; // insert item description data in HTML table format
}
echo $tableData;
}
else {
echo "Error: " . $sql . "<br>" . $severConnection->error;
}
}
/**
* get details of items added in cart from database and echo it in HTML formated table
*/
function UpdateCart(){
GLOBAL $severConnection; // secure database connection
$table = ""; // instantiate variable for HTML formated table
$tableData = '<tr><th>Product ID</th><th>Product Name</th><th>Product Price</th>'; // table header
foreach ($_SESSION['cart'] as $cart_item) { // loop to select each item added in the cart
$sql = "SELECT product_id, product_name, product_price FROM catalogue WHERE product_id= '$cart_item' ;"; // select query to get details of cart items from database
$result = mysqli_query($severConnection, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { // assign query result to associative array
$tablerow = '</td>'. '<td>'. $row['product_id']. '</td>'. '<td>'. $row['product_name']. '</td>'. '<td>'. $row['product_price']. '</td>'. '<td>'. '<button id="Removebutton" onclick="Add_RemoveItem(\'' . 'remove_item' . '\' , \'' . $row['product_id'] . '\' , \'' . 'cart_table'. '\' )">Remove Item</button>'; // insert query result in HTML formated table
$table .= '<tr>'. $tablerow . '</tr>' ;
}
}
}
echo $tableData .$table;
}
/**
* update databse if item is remove from cart
* item removed from cart is added to item in the database
*/
function AddToQuantity(){
GLOBAL $severConnection;
$cart_item = $_POST['remove_item']; // assign variable to item removal POST request value
$sql = "SELECT product_quantity FROM catalogue WHERE product_id= '$cart_item'; "; // select query to get the current quantity of item to add in database
$result = mysqli_query($severConnection, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$quantity = $row['product_quantity']+1; // add 1 to query result and assign it a variable
}
$sql = "UPDATE catalogue SET product_quantity= '$quantity' WHERE product_id= '$cart_item';"; // update item quantity in database with the new quantity
if (mysqli_query($severConnection, $sql)) {
echo "";
} else {
echo "Error updating record: " . mysqli_error($severConnection);
}
}else {
echo "Error updating record: " . $severConnection->error;
}
}
/**
* update databse if item is added to cart
* addition of item to cart removes it from database
*/
function RemoveFromQuantity(){
GLOBAL $severConnection;
$cart_item = $_POST['add_item']; // assign variable to item addition POST request value
$sql = "SELECT product_quantity FROM catalogue WHERE product_id='$cart_item' "; // select query to get the current quantity of item to remove in database
$result = mysqli_query($severConnection, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$quantity = $row['product_quantity']; // assign variable to quantity search result
if ($quantity < 1 ){ // if less than 1, no action taken
$quantity = $quantity;
}
else{ // if greater than 1, minus 1 from it
$quantity--;
}
}
$sql = "UPDATE catalogue SET product_quantity= '$quantity' WHERE product_id='$cart_item';"; // update database with the new quantity
if (mysqli_query($severConnection, $sql)) {
echo "";
} else {
echo "Error updating record: " . mysqli_error($severConnection);
}
}else {
echo "Error updating record: " . $severConnection->error;
}
}
/**
* get list of item categories from database
*/
function CategoryList(){
GLOBAL $severConnection;
$list = '';
// create additional category outside the list from database
$All = '<li id="Allcart" onclick="LoadItem(\'' . 'Category' . '\' , \'' . 'All' . '\' , \'' . 'Limit'. '\' , \'' . '7'. '\' , \'' . 'Offset'. '\' , \'' . '0'. '\' , \'' . 'table'. '\' )">All</li>'. '<br>';
$sql = "SELECT DISTINCT product_category FROM catalogue "; // select query to get category list from database
$result = mysqli_query($severConnection, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
for($i = 0; $i< count($row); $i++){ // loop through list of category and insert in HTML formated table
$list .= '<li id=\'' . $row[$i]. '\' onclick="LoadItem(\'' . 'Category' . '\' , \'' . $row[$i] . '\' , \'' . 'Limit'. '\' , \'' . '7'. '\' , \'' . 'Offset'. '\' , \'' . '0'. '\' , \'' . 'table'. '\' )" >'.$row[$i].'</li>'.'<br>';
}
}
}
echo $All.$list;
}
/**
* check item quantity and alert display out of stock if below 1
*/
function quantityCheck($check){
if ( $check <= 0 ){
$check = '<span style="color:#FF0000;">Out of Stock</span>';
}
else{
$check = $check;
}
return $check;
}
/**
* check item quantity and disable add button if less than 1
*/
function CartCheck($check, $input){
$output = '';
if ( $check <= 0 ){
$output = '<button disabled onclick="Add'.$check.'()"><span style="color:#FF0000;">Out of Stock</span></button>';
}
else{
$output = '<button id="addbutton" onclick="Add_RemoveItem(\'' . 'add_item' . '\' , \'' . $input . '\' , \'' . 'cart_table'. '\' )">Add Item</button>';
}
return $output;
}
?>