-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
executable file
·192 lines (192 loc) · 5.29 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
<?php
/**
* Created by PhpStorm.
* User: karti
* Date: 01-Nov-17
* Time: 3:41 PM
*/
include 'dbconfig.php';
session_start();
if (isset($_SESSION['cart']))
$cart = $_SESSION['cart'];
else
$cart = [];
$total;
$order = rand(1,100);
if(isset($_POST['add']) && $_POST['add'] != 2) {
$id = $_POST['id'];
$no = $_POST['quantity'];
if ($_POST['add'] == 1)
addToCart($id, $no);
elseif ($_POST['add'] == 0)
removeFromCart($id);
}
if(isset($_POST['place_ord'])){
if(isset($_POST['pref']))
placeOrder($order,$_POST['pref']);
else
placeOrder($order," ");
exit();
}
if (isset($_POST['viewOrd'])){
viewOrder();
exit();
}
showCart();
function addToCart($item_id,$quantity){
global $mysqli,$cart;
$sql = "SELECT * FROM menu WHERE item_id=$item_id";
if (!$mysqli->query($sql))
echo $mysqli->error;
$res = $mysqli->query($sql);
$row = $res->fetch_assoc();
if (array_key_exists($item_id,$cart)){
$cart[$item_id][2] = $quantity;
$cart[$item_id][3] = $cart[$item_id][1] * $quantity;
$_SESSION['cart'] = $cart;
return;
}
$total = $row['cost']*$quantity;
$arr = array($row['item_id'] => array($row['Name'],$row['cost'],$quantity,$total));
$cart = $cart + $arr;
$_SESSION['cart'] = $cart;
}
function removeFromCart($id){
global $cart;
if (array_key_exists($id,$cart)){
$cart[$id][2]--;
if ($cart[$id][2] == '0') {
unset($cart[$id]);
}
}
else
return;
$_SESSION['cart'] = $cart;
}
function showCart(){
global $cart,$total;
if (count($cart) == 0) {
echo "No items in the cart";
}
else {
$html = <<<EOT
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
EOT;
$total = 0;
foreach($cart as $key=>$value){
$html .= <<<EOT
<tr value="{$key}">
<th>{$value[0]}</th>
<th>{$value[1]}</th>
<th>{$value[2]}</th>
<th>{$value[3]}</th>
</tr>
EOT;
$total += $value[3];
}
$html .= "<tr><th>Total:{$total}</th></tr></tbody></table>";
$html .= "<textarea rows='3' placeholder='preferences....' name='pref' id='pref' class='form-control'/><br>";
$html .= "<button class='btn btn-danger' id='place-order'>Place Order</button>";
echo $html;
}
}
function assignWaiter(){
global $mysqli;
if ($res = $mysqli->query("CALL allocateWaiter()")){
$row = $res->fetch_all();
$n = $res->num_rows;
if ($n != 0) {
$i = rand(0,3);
echo $i;
mysqli_free_result($res);
mysqli_next_result($mysqli);
return $row[$i][0];
}
else
return 3;
}
else
$mysqli->error;
}
function placeOrder($ord_id,$pref){
global $mysqli,$cart;
if(!isset($_SESSION['tableno'])) {
echo "0 You cannot place an order until you book a table";
return;
}
$waiter = (int)assignWaiter();
foreach ($cart as $key=>$value){
$sql = "INSERT INTO orders VALUES($ord_id,$key,$waiter,{$_SESSION['tableno']},1,'$pref',$value[2],$value[3])";
if ($mysqli->query($sql)) {
echo "1";
unset($_SESSION['cart']);
unset($cart);
}
else
echo $mysqli->error;
}
}
function viewOrder(){
global $mysqli;
$q = "SELECT order_no FROM orders WHERE table_no={$_SESSION['tableno']} GROUP BY order_no";
$result = $mysqli->query($q);
$html = "";
while($row = $result->fetch_assoc()){
$total = 0;
$sql = "SELECT * FROM orders WHERE table_no={$_SESSION['tableno']} AND order_no={$row['order_no']}";
$res = $mysqli->query($sql);
$href = "collapse".$row['order_no'];
$html .= <<<EOT
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#{$href}">{$row['order_no']}</a>
</h4>
</div>
<div id="{$href}" class="panel-collapse collapse">
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Total</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
EOT;
while($rows = $res->fetch_assoc()){
$q1 = "SELECT Name FROM menu WHERE item_id={$rows['item_id']}";
$r = $mysqli->query($q1);
$name = $r->fetch_array()[0];
$total += $rows['cost'];
$html .= <<<EOT
<tr>
<th>{$name}</th>
<th>{$rows['cost']}</th>
<th>{$rows['quantity']}</th>
</tr>
EOT;
}
$html .= <<<EOT
<tr><th>Total:{$total}</th></tr>
</tbody>
</table>
</div>
</div>
</div>
EOT;
}
$html .= "</div><br><a href='billing.php'><button class='btn btn-done'>Pay and leave</button></a>";
echo $html;
}