-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrestaurant.php
More file actions
executable file
·844 lines (698 loc) · 37.7 KB
/
restaurant.php
File metadata and controls
executable file
·844 lines (698 loc) · 37.7 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
<html>
<head>
<title>Restaurant</title>
</head>
<link rel="stylesheet" href="style.css">
<style>
* {
font-family: sans-serif;
}
</style>
<body class = "restaurantBody">
<div class = "restaurant">
<h1>Restaurant Page</h1>
</div>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<hr>
<h2>Become A Partner Restaurant!</h2>
<form method="POST" action="restaurant.php">
<input type="hidden" id="insertRestaurant" name="insertRestaurant">
<label>Restaurant ID: </label><input type="text" name="restaurant_id"> <br /><br />
<label>Name: </label> <input type="text" name="name"> <br /><br />
<label>Postal Code: </label><input type="text" name="postal_code"> <br /><br />
<label>Street Address: </label><input type="text" name="street_address"> <br /><br />
<label>Category: </label><input type="text" name="category"> <br /><br />
<label>City: </label><input type="text" name="city"> <br /><br />
<label>Province: </label><input type="text" name="province"> <br /><br />
<input type="submit" value="Submit" name="insertSubmit"></p>
</form>
<hr>
<h2>Check Your Restaurant's Current Info:</h2>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkRestaurant" name="checkRestaurant">
<label>Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="submit" value="Submit" name="displayRestaurant"></p>
</form>
<hr>
<h2>Update Your Restaurant's Info:</h2>
<form method="POST" action="restaurant.php">
<input type="hidden" id="updateRestaurantInfo" name="updateRestaurantInfo">
<label>Your Restaurant ID:</label> <input type="text" name="restaurant_id"> <br /><br />
<input type="checkbox" name="updateName" id="updateName">
<label for="updateName">
New Name: <input type="text" name="newName">
</label><br /><br />
<input type="checkbox" name="updateStreetAddress" id="updateStreetAddress">
<label for="updateStreetAddress">
New Street Address: <input type="text" name="newStreetAddress">
</label><br /><br />
<input type="checkbox" name="updateCategory" id="updateCategory">
<label for="updateCategory">
New Category: <input type="text" name="newCategory">
</label><br /><br />
<input type="checkbox" name="updatePostalCode" id="updatePostalCode">
<label for="updatePostalCode">
Move To A New Location Within The Same City?<br /><br />
New Postal Code: <input type="text" name="newPostalCode1">
</label><br /><br />
<input type="checkbox" name="updateCityAndProvince" id="updateCityAndProvince">
<label for="updateCityAndProvince">
Move To Another City? <br /><br />
New Postal Code: <input type="text" name="newPostalCode2">
New City: <input type="text" name="newCity">
New Province: <input type="text" name="newProvince">
</label><br /><br />
<input type="submit" value="Submit" name="updateSubmit"></p>
</form>
<hr>
<h2>Delete Your Restaurant From the App:</h2>
<form method="POST" action="restaurant.php">
<input type="hidden" id="deleteRestaurant" name="deleteRestaurant">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="submit" value="Submit" name="deleteSubmit"></p>
</form>
<hr>
<h2>Manage Your Restaurant's Dishes/Menu:</h2>
<h3>View Your Restaurant's Dishes:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkDishes" name="checkDishes">
<label>Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="submit" value="Submit" name="displayDishes"></p>
</form>
<h3>Add New Dishes to Your Restaurant's Menu:</h3>
<form method="POST" action="restaurant.php">
<input type="hidden" id="insertMenuItem" name="insertMenuItem">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<label>Dish Name: </label><input type="text" name="name"><br /><br />
<label>Dish Description: </label><input type="text" name="description"><br /><br />
<label>Ingredient: </label><input type="text" name="ingredient"><br /><br />
<label>Category: </label><input type="text" name="category"><br /><br />
<label>Price: </label><input type="number" name="price" step="0.01"><br /><br />
<input type="submit" value="Submit" name="insertSubmit"></p>
</form>
<h3>Change the Information of a Dish in Your Restaurant's Menu:</h3>
<form method="POST" action="restaurant.php">
<input type="hidden" id="updateMenuItem" name="updateMenuItem">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"> <br /><br />
<label>Dish Name: </label><input type="text" name="name"> <br /><br />
<input type="checkbox" name="updateName" id="updateName">
<label for="updateName">
New Name: <input type="text" name="newName">
</label><br /><br />
<input type="checkbox" name="updateDescription" id="updateDescription">
<label for="updateDescription">
New Description: <input type="text" name="newDescription">
</label><br /><br />
<input type="checkbox" name="updateIngredient" id="updateIngredient">
<label for="updateIngredient">
New Ingredient: <input type="text" name="newIngredient">
</label><br /><br />
<input type="checkbox" name="updateCategory" id="updateCategory">
<label for="updateCategory">
New Category: <input type="text" name="newCategory">
</label><br /><br />
<input type="checkbox" name="updatePrice" id="updatePrice">
<label for="updatePrice">
New Price: <input type="number" name="newPrice" step="0.01">
</label><br /><br />
<input type="submit" value="Submit" name="updateSubmit"></p>
</form>
<h3>Delete a Dish from Your Restaurant's Menu:</h3>
<form method="POST" action="restaurant.php">
<input type="hidden" id="deleteMenuItem" name="deleteMenuItem">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<label>Dish Name: </label><input type="text" name="name"><br /><br />
<input type="submit" value="Submit" name="deleteSubmit"></p>
</form>
<hr>
<h2>Search For Orders Prepared By Your Restaurant!</h2>
<h3>View All Orders Your Restaurant Has Prepared:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkAllOrders" name="checkAllOrders">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="submit" name="displayAllOrders"></p>
</form>
<h3>Search For Orders That Have a Food Subtotal Between A Certain Range:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkPriceOrders" name="checkPriceOrders">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="checkbox" name="fromPrice" id="fromPrice">
<label for="fromPrice">
Higher Than: <input type="number" name="min" step="0.01">
</label><br /><br />
<input type="checkbox" name="toPrice" id="toPrice">
<label for="toPrice">
Lower Than: <input type="number" name="max" step="0.01">
</label><br /><br />
<input type="submit" name="displayPriceOrders"></p>
</form>
<hr>
<h2>View All Orders That Have Been Placed With Your Restaurant:</h2>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkAllCustomer" name="checkAllCustomer">
<label>Your Restaurant ID: </label><input type="text" name="restaurant_id"><br /><br />
<input type="submit" name="displayCustomer"></p>
</form>
<hr>
<h2>Display All Restaurant Related Tables:</h2>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkAllTables" name="checkAllTables">
<input type="submit" name="displayAllTables"></p>
</form>
<hr>
<h2>Hardcoded Queries</h2>
<h3>Find All Customers Who Have Ordered From All Restaurants in Montreal, Quebec:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkHardCodeCustomerQuery" name="checkHardCodeCustomerQuery">
<input type="submit" name="displayHardCodeCustomerQuery"></p>
</form>
<h3>Find All Customers Who Have Placed At Least Three Orders With a Food Subtotal Greater Than $20:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkMoreOrderCustomerQuery" name="checkMoreOrderCustomerQuery">
<input type="submit" name="displayMoreOrderCustomerQuery"></p>
</form>
<h3>For All Restaurants That Have a Rating That is Higher Than a Specific Value, Return the Average Food Subtotal for
the Orders Made at Each of Those Restaurants:</h3>
<form method="GET" action="restaurant.php">
<input type="hidden" id="checkHigherRatingRestaurantQuery" name="checkHigherRatingRestaurantQuery">
Rating: <input type="number" name="rating" step="0.1" min="0" max="10"> <br /><br />
<input type="submit" name="displayHigherRatingRestaurantQuery"></p>
</form>
<a href="mainpage.php">Return to Main Page</a>
<br>
<br>
<?php
$success = True;
$db_conn = NULL;
$show_debug_alert_messages = False;
function debugAlertMessage($message) {
global $show_debug_alert_messages;
if ($show_debug_alert_messages) {
echo "<script type='text/javascript'>alert('" . $message . "');</script>";
}
}
function executePlainSQL($cmdstr) {
global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn);
echo htmlentities($e['message']);
$success = False;
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = oci_error($statement);
echo htmlentities($e['message']);
$success = False;
}
return $statement;
}
function executeBoundSQL($cmdstr, $list) {
global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn);
echo htmlentities($e['message']);
$success = False;
}
foreach ($list as $tuple) {
foreach ($tuple as $bind => $val) {
OCIBindByName($statement, $bind, $val);
unset ($val);
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = OCI_Error($statement);
echo htmlentities($e['message']);
echo "<br>";
$success = False;
}
}
}
function connectToDB() {
global $db_conn;
$db_conn = OCILogon("ora_vicche04", "a13090618", "dbhost.students.cs.ubc.ca:1522/stu");
if ($db_conn) {
debugAlertMessage("Database is Connected");
return true;
} else {
debugAlertMessage("Cannot connect to Database");
$e = OCI_Error();
echo htmlentities($e['message']);
return false;
}
}
function disconnectFromDB() {
global $db_conn;
debugAlertMessage("Disconnect from Database");
OCILogoff($db_conn);
}
//Insert Menu
function handleInsertMenu() {
global $db_conn;
$name = $_POST['name'];
$restaurant_id = $_POST['restaurant_id'];
$name_is_in_use = executePlainSQL("SELECT count(*) FROM Menu_Items_Has WHERE restaurant_id=" . $restaurant_id . "AND name = '" . $name . "'");
$row = OCI_Fetch_Array($name_is_in_use, OCI_BOTH);
if ($row[0] != 0) {
$err_message = 'This Dish Name is already is use. Please choose another one.';
echo "<script type='text/javascript'> swal('Error!', '$err_message', 'error'); </script>";
// To not make the error message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
} else {
$tuple1 = array (
":bind6" => $restaurant_id,
":bind1" => $name,
":bind2" => $_POST['description'],
":bind3" => $_POST['ingredient'],
":bind4" => $_POST['category'],
":bind5" => $_POST['price'],
);
$alltuples1 = array (
$tuple1
);
executeBoundSQL("insert into Menu_Items_Has values (:bind1, :bind2, :bind3, :bind4, :bind5, :bind6)", $alltuples1);
$message = 'Menu Item Was Successfully Added!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
}
OCICommit($db_conn);
}
//Insert Restaurant
function handleInsertRestaurant() {
global $db_conn;
$restaurant_id = $_POST['restaurant_id'];
// Check if restaurant_id field was left empty
if (strlen($restaurant_id) == 0) {
$err_message = 'Restaurant ID cannot be empty. Please enter a valid restaurant ID and try again.';
echo "<script type='text/javascript'> swal('Error!', '$err_message', 'error'); </script>";
// To not make the error message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
}
else {
$id_is_in_use = executePlainSQL("SELECT count(*) FROM Restaurant WHERE restaurant_id = $restaurant_id");
$row = OCI_Fetch_Array($id_is_in_use, OCI_BOTH);
if ($row[0] != 0) {
$err_message = 'This Restaurant ID is already is use. Please choose another one.';
echo "<script type='text/javascript'> swal('Error!', '$err_message', 'error'); </script>";
// To not make the error message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
} else {
$postal_code = $_POST['postal_code'];
$tuple1 = array (
":bind1" => $restaurant_id,
":bind2" => $postal_code,
":bind3" => $_POST['name'],
":bind4" => $_POST['category'],
":bind5" => 10.0,
":bind6" => $_POST['street_address'],
);
$alltuples1 = array (
$tuple1
);
$city = $_POST['city'];
$province = $_POST['province'];
$unique_pos_code = executePlainSQL("select count(*) from Address where postal_code = '$postal_code'");
$row = OCI_Fetch_Array($unique_pos_code, OCI_BOTH);
if ($row[0] == 0) {
executePlainSQL("INSERT into Address VALUES ('$postal_code', '$city', '$province')");
}
executeBoundSQL("insert into Restaurant values (:bind1, :bind2, :bind3, :bind4, :bind5, :bind6)", $alltuples1);
$message = 'Restaurant Was Successfully Registered!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
}
}
OCICommit($db_conn);
}
//print results from a select restaurant
function printRestaurant($result) { //prints results from a select statement
echo "<br>Retrieved data from Restaurant:<br>";
echo "<table>";
echo "<tr><th>Restaurant_ID</th><th>Postal_Code</th><th>Name</th><th>Category</th><th>Rating</th><th>Street_Address</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row["RESTAURANT_ID"] . "</td><td>" . $row["POSTAL_CODE"] . "</td><td>" . $row["NAME"] . "</td><td>" . $row["CATEGORY"] . "</td><td>" . $row["RATING"] . "</td><td>" . $row["STREET_ADDRESS"] . "</td></tr>"; //or just use "echo $row[0]"
}
echo "</table>";
}
//handle sql statement for select specific restaurant
function handleSelectRestaurant() {
global $db_conn;
$value = $_GET['restaurant_id'];
$txt = "SELECT * FROM Restaurant WHERE restaurant_id = " . $value;
$result = executePlainSQL($txt);
printRestaurant($result);
}
//handle sql for deleting the specific row in menu
function handleDeleteMenuItem() {
global $db_conn;
$restaurant_id = $_POST['restaurant_id'];
$dishname = $_POST['name'];
executePlainSQL("DELETE FROM Menu_Items_Has WHERE restaurant_id=" . $restaurant_id . "AND name = '" . $dishname . "'");
$message = 'Menu Item Was Successfully Deleted!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
OCICommit($db_conn);
}
//handle sql for deleting the specific row in restaurant
function handleDeleteRestaurant() {
global $db_conn;
$restaurant_id = $_POST['restaurant_id'];
executePlainSQL("DELETE FROM Restaurant WHERE restaurant_id=" . $restaurant_id);
$message = 'Your Restaurant Was Successfully Deleted!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
OCICommit($db_conn);
}
//update menu
function handleUpdateMenuItem() {
global $db_conn;
$restaurant_id = $_POST['restaurant_id'];
$olddishname = $_POST['name'];
if (isset($_POST['updateDescription'])) {
$newDescription = $_POST['newDescription'];
executePlainSQL("UPDATE Menu_Items_Has SET description ='" . $newDescription . "' WHERE restaurant_id=" . $restaurant_id . " AND name = '" . $olddishname . "'");
}
if (isset($_POST['updateIngredient'])) {
$newIngredient = $_POST['newIngredient'];
executePlainSQL("UPDATE Menu_Items_Has SET ingredient ='" . $newIngredient . "' WHERE restaurant_id=" . $restaurant_id . " AND name = '" . $olddishname . "'");
}
if (isset($_POST['updateCategory'])) {
$newCategory = $_POST['newCategory'];
executePlainSQL("UPDATE Menu_Items_Has SET category ='" . $newCategory . "' WHERE restaurant_id = " . $restaurant_id . " AND name = '" . $olddishname . "'");
}
if (isset($_POST['updatePrice'])) {
$newPrice = $_POST['newPrice'];
executePlainSQL("UPDATE Menu_Items_Has SET price ='" . $newPrice . "' WHERE restaurant_id=" . $restaurant_id . " AND name = '" . $olddishname . "'");
}
if (isset($_POST['updateName'])) {
$newName = $_POST['newName'];
executePlainSQL("UPDATE Menu_Items_Has SET name ='" . $newName . "' WHERE restaurant_id = " . $restaurant_id . " AND name = '" . $olddishname . "'");
}
$message = 'Menu Item Was Successfully Updated!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
OCICommit($db_conn);
}
//update restaurant
function handleUpdateRestaurant() {
global $db_conn;
$restaurant_id = $_POST['restaurant_id'];
if (isset($_POST['updateName'])) {
$newname = $_POST['newName'];
executePlainSQL("UPDATE Restaurant SET name ='" . $newname . "' WHERE restaurant_id = " . $restaurant_id);
}
if (isset($_POST['updateStreetAddress'])) {
$newStreetAddress = $_POST['newStreetAddress'];
executePlainSQL("UPDATE Restaurant SET street_address ='" . $newStreetAddress . "' WHERE restaurant_id=" . $restaurant_id);
}
if (isset($_POST['updateCategory'])) {
$newCategory = $_POST['newCategory'];
executePlainSQL("UPDATE Restaurant SET category ='" . $newCategory . "' WHERE restaurant_id=" . $restaurant_id );
}
if (isset($_POST['updatePostalCode'])) {
$newPostalCode = $_POST['newPostalCode1'];
$postal_code;
$city;
$province;
$result = executePlainSQL("SELECT A.postal_code, A.city, A.province
FROM Restaurant R, Address A WHERE A.postal_code = R.postal_code AND R.restaurant_id =" . $restaurant_id);
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
$city = $row[1];
$province = $row[2]; // get saved province and city
}
$tuple = array (
":bind1" => $newPostalCode,
":bind2" => $city,
":bind3" => $province
);
$alltuples = array (
$tuple
);
executeBoundSQL("insert into Address values (:bind1, :bind2, :bind3)", $alltuples);
executePlainSQL("UPDATE Restaurant SET postal_code='" . $newPostalCode. "' WHERE restaurant_id=" . $restaurant_id);
}
if (isset($_POST['updateCityAndProvince'])) {
$newPostalCode = $_POST['newPostalCode2'];
$newCity = $_POST['newCity'];
$newProvince = $_POST['newProvince'];
$tuple = array (
":bind1" => $newPostalCode,
":bind2" => $newCity,
":bind3" => $newProvince
);
$alltuples = array (
$tuple
);
executeBoundSQL("insert into Address values (:bind1, :bind2, :bind3)", $alltuples);
executePlainSQL("UPDATE Restaurant SET postal_code='" . $newPostalCode. "' WHERE restaurant_id=" . $restaurant_id);
}
$message = 'Restaurant Information Was Successfully Updated!';
echo "<script type='text/javascript'> swal('Success!', '$message', 'success'); </script>";
// To not make the message appear again after the page is refreshed
echo "<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>";
OCICommit($db_conn);
}
//handle the sql statement for selecting all the orders from the restaurant
function handleDisplayAllOrders() {
global $db_conn;
$restaurant_id = $_GET['restaurant_id'];
$result = executePlainSQL("SELECT order_number, restaurant_id,customer_id, date_placed, food_subtotal FROM Orders WHERE restaurant_id=" . $restaurant_id);
printAllOrderResults($result);
}
//handle the sql statement for selecting all the orders within the range(food_subtotal) from the restaurant
function handleDisplayOrdersWithInRange() {
global $db_conn;
$restaurant_id = $_GET['restaurant_id'];
$max = PHP_INT_MAX;
$min = 0;
if (isset($_GET['fromPrice'])) {
$min = $_GET['min'];
}
if (isset($_GET['toPrice'])) {
$max = $_GET['max'];
}
$result = executePlainSQL("SELECT order_number, restaurant_id, customer_id, date_placed, food_subtotal FROM Orders
WHERE restaurant_id = " . $restaurant_id . " AND food_subtotal <= " . $max . " AND food_subtotal >= ". $min);
printAllOrderResults($result);
}
//print the orders
function printAllOrderResults($result) {
echo "<br>Retrieved Orders from Restaurant:<br>";
echo "<table>";
echo "<tr><th>order_number</th><th>restaurant_id</th><th>customer_id</th><th>date_placed</th><th>food_subtotal</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td></tr>";
}
echo "</table>";
}
function handleDisplayAllTables() {
global $db_conn;
$result = executePlainSQL("SELECT * FROM Restaurant");
printRestaurant($result);
$result = executePlainSQL("SELECT order_number, restaurant_id, customer_id, date_placed, food_subtotal FROM Orders");
printAllOrderResults($result);
$result = executePlainSQL("SELECT * FROM Menu_Items_Has");
printAllMenuItems($result);
$result = executePlainSQL("SELECT * FROM Address");
printAllAddress($result);
}
function handleDisplayDishes() {
global $db_conn;
$restaurant_id = $_GET['restaurant_id'];
$result = executePlainSQL("SELECT * FROM Menu_Items_Has WHERE restaurant_id = " . $restaurant_id);
printAllMenuItems($result);
}
function printAllAddress($result) {
echo "<br>Retrieved Dishes from Address:<br>";
echo "<table>";
echo "<tr><th>Postal_Code</th><th>City</th><th>Province</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td></tr>";
}
echo "</table>";
}
function printAllMenuItems($result) {
echo "<br>Retrieved Dishes from your Restaurant:<br>";
echo "<table>";
echo "<tr><th>Name</th><th>Description</th><th>Ingredient</th><th>Category</th><th>Price</th><th>Restaurant_ID</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td></tr>";
}
echo "</table>";
}
function handleDisplayCustomer() {
global $db_conn;
$restaurant_id = $_GET['restaurant_id'];
$result = executePlainSQL("SELECT o.order_number, c.customer_id, c.email, c.name, c.phone_number, c.street_address
FROM Customer c, Restaurant r, Orders o
WHERE o.customer_id = c.customer_id AND r.restaurant_id = o.restaurant_id AND r.restaurant_id = " . $restaurant_id);
printAllCustomer($result);
}
function printAllCustomer($result) {
echo "<br>Retrieved Information About All Orders Placed With Your Restaurant:<br>";
echo "<table>";
echo "<tr><th>Order_Number</th><th>Customer_ID</th><th>Email</th><th>Name</th><th>Phone_Number</th><th>Street_Address</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td></tr>";
}
echo "</table>";
}
function handlePOSTRequest() {
if (connectToDB()) {
if (array_key_exists('deleteMenuItem', $_POST)) {
handleDeleteMenuItem();
}else if (array_key_exists('updateMenuItem', $_POST)) {
handleUpdateMenuItem();
}else if (array_key_exists('insertRestaurant', $_POST)) {
handleInsertRestaurant();
}else if (array_key_exists('updateRestaurantInfo', $_POST)) {
handleUpdateRestaurant();
}else if (array_key_exists('deleteRestaurant', $_POST)) {
handleDeleteRestaurant();
}else if (array_key_exists('insertMenuItem', $_POST)) {
handleInsertMenu();
}
disconnectFromDB();
}
}
// Find all customers who have ordered from all restaurants in Montreal, Quebec.
function handleDisplayHardCodeCustomerQuery() {
global $db_conn;
$result = executePlainSQL("SELECT C.name, C.customer_id
FROM Customer C
WHERE NOT EXISTS ((SELECT R.restaurant_id
FROM Restaurant R, Address A
WHERE R.postal_code = A.postal_code
AND A.city = 'Montreal'
AND A.province = 'Quebec')
MINUS (SELECT O.restaurant_id
FROM Orders O, Restaurant R2, Address A2
WHERE O.restaurant_id = R2.restaurant_id
AND O.customer_id = C.customer_id
AND R2.postal_code = A2.postal_code
AND A2.city = 'Montreal'
AND A2.province = 'Quebec'))");
echo "<br>Find all customers who have ordered from all restaurants in Montreal, Quebec:<br>";
echo "<table>";
echo "<tr><th>Name</th><th>Customer ID</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
}
echo "</table>";
}
function handleDisplayHigherRatingRestaurantQuery() {
global $db_conn;
$rating = $_GET['rating'];
$result = executePlainSQL("SELECT avg_subtotal, R.restaurant_id, R.name, R.rating
FROM Restaurant R, (SELECT R2.restaurant_id AS res_id, avg(food_subtotal) AS avg_subtotal
FROM Restaurant R2 JOIN Orders O
ON R2.restaurant_id = O.restaurant_id
WHERE R2.rating > '$rating'
GROUP BY R2.restaurant_id)
WHERE R.restaurant_id = res_id");
echo "<br>Retrieved Restaurant and Order Info: <br>";
echo "<table>";
echo "<tr><th>Average Food Subtotal</th><th>Restaurant ID</th><th>Name</th><th>Rating</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td></tr>";
}
echo "</table>";
}
//Find the IDs of customers who have placed at least three orders, where only orders with a food subtotal > 20 are considered.
function handleDisplayMoreOrderCustomerQuery() {
global $db_conn;
$result = executePlainSQL("SELECT DISTINCT C.customer_id, COUNT(*)
FROM Orders O, Customer C
WHERE O.customer_id = C.customer_id AND O.food_subtotal > 20
GROUP BY C.customer_id
HAVING COUNT(*) > 2");
echo "<br>Find IDs of Customers Who Have Placed At Least Three Orders, With Food Subtotal Higher Than $20:<br>";
echo "<table>";
echo "<tr><th>Customer ID</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td></tr>";
}
echo "</table>";
}
function handleGETRequest() {
if (connectToDB()) {
if (array_key_exists('displayAllOrders', $_GET)) {
handleDisplayAllOrders();
} else if (array_key_exists('displayAllTables', $_GET)) {
handleDisplayAllTables();
} else if (array_key_exists('displayCustomer', $_GET)) {
handleDisplayCustomer();
} else if (array_key_exists('displayRestaurant', $_GET)) {
handleSelectRestaurant();
} else if (array_key_exists('displayPriceOrders', $_GET)) {
handleDisplayOrdersWithInRange();
} else if (array_key_exists('displayDishes', $_GET)) {
handleDisplayDishes();
} else if (array_key_exists('displayHardCodeCustomerQuery', $_GET)) {
handleDisplayHardCodeCustomerQuery();
} else if (array_key_exists('displayMoreOrderCustomerQuery', $_GET)) {
handleDisplayMoreOrderCustomerQuery();
} else if (array_key_exists('displayHigherRatingRestaurantQuery', $_GET)) {
handleDisplayHigherRatingRestaurantQuery();
}
disconnectFromDB();
}
}
if (isset($_POST['insertSubmit']) || isset($_POST['updateSubmit']) || isset($_POST['deleteSubmit'])) {
handlePOSTRequest();
} else if (isset($_GET['checkAllOrders']) || isset($_GET['checkAllCustomer'])
|| isset($_GET['checkRestaurant']) || isset($_GET['checkAllTables'])) {
handleGETRequest();
} else if (isset($_GET['checkPriceOrders']) || isset($_GET['checkDishes'])
|| isset($_GET['checkHardCodeCustomerQuery']) || isset($_GET['checkMoreOrderCustomerQuery']) || isset($_GET['checkHigherRatingRestaurantQuery'])) {
handleGETRequest();
}
?>
</body>
</html>