-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdmin_orders.php
379 lines (348 loc) · 11 KB
/
Admin_orders.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<html>
<head>
<title> Admin - Orders</title>
<link rel="icon" href="images/admin.jfif" type="image/x-icon">
<link href="CSS/simple-sidebar.css" rel="stylesheet">
<style>
table {
border-collapse: collapse;
margin-top: 15px;
width: 100%;
text-align: center;
}
th, td {
text-align: left;
padding: 8px 0px;
text-align: center;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #292b2c;
color: white;
}
</style>
</head>
<?php
session_start();
if (!$_SESSION['loggedIn'] || $_SESSION['user']['Role'] != 2) {
echo "<script> location.href='home.php'; </script>";
}
include "menu.php";
?>
<body>
<div class="d-flex" id="wrapper">
<div class="bg-dark border-right" id="sidebar-wrapper">
<div class="list-group list-group-flush bg-dark">
<a href="Admin_products.php" class="list-group-item list-group-item-action bg-dark text-light show"><span class="text-nowrap"><i class="fa fa-plus-square"></i> Products</a></span>
<a href="Admin_users.php" class="list-group-item list-group-item-action bg-dark text-light"><span class="text-nowrap"><i class="fa fa-user"></i> Users</a></span>
<a href="Admin_orders.php" class="list-group-item list-group-item-action bg-secondary text-light"><span class="text-nowrap"><i class="fa fa-cog"></i> Orders</a></span>
<a href="Admin_contactus.php" class="list-group-item list-group-item-action bg-dark text-light"><span class="text-nowrap"><i class="fa fa-cog"></i> contact us</a></span>
</div>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="row d-flex justify-content-center">
<!--Grid column-->
<div class="col-md-6" style="margin-top: 15px;">
<div class="input-group rounded">
<input type="search" id="searchBar" class="form-control rounded" placeholder="Search" aria-label="Search"
aria-describedby="search-addon" />
<span onclick="search()" style="margin-left: 10px;" class="btn input-group-text border-0" id="search-addon">
<i class="fa fa-search"></i>
</span>
<span onclick="show()" style="margin-left: 10px;" class="btn input-group-text border-0" id="searchoptions">
Show search options
</span>
</div>
</div>
</div>
<div class="row d-flex justify-content-center" >
<div class="col-md-3 fade-in" style="display: none;" id="options">
Search by:
<div class="form-check">
<input class="form-check-input" type="radio" name="filter" checked>
<label class="form-check-label" for="customernameRadio">
Customer name
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="filter" id="amountofproductsRadio">
<label class="form-check-label" for="amountofproductsRadio">
Amount of products
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="filter" id="tpriceRadio">
<label class="form-check-label" for="tpriceRadio">
Total price
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="filter" id="addressRadio">
<label class="form-check-label" for="addressRadio">
Address
</label>
</div>
</div>
</div>
<div class="container-fluid">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$DB = "mydb";
$conn = mysqli_connect($servername, $username, $password, $DB);
if (!$conn) {
error_log(mysqli_connect_error(),3,"../errors/db-errors.log");
die("Connection failed: " .mysqli_connect_error());
}
if (isset($_GET['delete'])) {
$valuesToDelete = explode (",", $_GET['delete']);
for ($i = 0; $i < count($valuesToDelete); $i++) {
$v = $valuesToDelete[$i];
//echo "<script>alert('".$v."');</script>";
$conn->query("DELETE FROM invoice WHERE Id=$v");
}
}
if (isset($_GET['search'])) {
$val = $_GET['search'];
$val2 = $_GET['searchBy'];
if ($val2 == "amount") {
$query1 = $conn->query("SELECT Id,ProductQuantities FROM invoice");
$amountIDS = array();
while ($row = $query1->fetch_assoc()) {
$values = array_map("intval", explode(",", $row["ProductQuantities"]));
$am = 0;
for ($i = 0; $i < count($values); $i++) {
$am += $values[$i];
}
if ($am == $val) {
array_push($amountIDS, $row["Id"]);
}
}
$res = $conn->query("SELECT * FROM invoice WHERE Id IN (".implode(",",$amountIDS).")");
} else if ($val2 == "price") {
$res = $conn->query("SELECT * FROM invoice WHERE TotalPrice='$val'");
} else if ($val2 == "address") {
$res = $conn->query("select * from invoice where Address like '%$val%' OR Address like '%$val' OR Address like '$val%'");
} else {
$query = $conn->query("select Id from user where firstName like '%$val%' OR firstName like '%$val' OR firstName like '$val%'");
$custIds = "";
while ($row = $query->fetch_assoc()) {
$custIds = $custIds."".$row["Id"].",";
}
$res = $conn->query("SELECT * FROM invoice WHERE customerId IN ('".$custIds."')");
}
} else {
$res = $conn->query("SELECT * FROM invoice");
}
if (!$res) {
//die "Query failed: (" . $res->errno . ") " . $res->error;
}
$rownumber = 1;
$page = 1;
$firstrow = 0;
echo "<table style='width:100%'>\n";
if (isset($_GET['page'])) {
$do = $_GET['page'];
$page = $do;
}
while ($row = $res->fetch_assoc()) {
if (0 == $firstrow) {
/* first result set row? look at the keys=column nanes */
echo "<tr>";
foreach (array_keys($row) as $colname) {
if ($colname == "reg_date") {
echo "<th>date added</th>";
} else if ($colname == "Id") {
echo "<th> #</th>";
} else if ($colname == "productIds") {
echo "<th>Products</th>";
} else if ($colname == "ProductQuantities") {
echo "<th>Amount of products</th>";
} else if ($colname == "customerId") {
echo "<th>Customer name</th>";
} else if ($colname == "TotalPrice") {
echo "<th>Total price</th>";
} else {
echo "<th>$colname</th>";
}
}
echo "<th>Action</th>";
echo "</tr>\n";
}
$firstrow = 1;
echo "<tr>";
$where = 0;
$id = 0;
foreach (array_values($row) as $colval) {
if (($rownumber <= $page*10 && abs($page*10 - $rownumber < 10))) {
if ($where == 0) {
$id = $colval;
echo "<td> <input type='checkbox' id='check".$id."' onclick='checkBoxes(".$id.")' name='select' value='".$id."'></td>";
} else if ($where == 1) {
$proudctsByID = explode (",", $colval);
echo "<td>";
for ($i = 0; $i < count($proudctsByID); $i++) {
//echo $proudctsByID[$i]." ";
echo "<a href='Admin_editproduct.php?Id=".$proudctsByID[$i]."'>".$proudctsByID[$i]."</a>";
if ((count($proudctsByID) - $i) > 1) {
echo ", ";
}
}
echo "</td>";
} else if ($where == 2) {
$query = $conn->query("SELECT firstName FROM user WHERE Id=".$colval."");
$accName = $query->fetch_assoc();
$n = $accName['firstName'];
echo "<td><a href='Admin_edituser.php?Id=$colval'>$n</a></td>";
} else if ($where == 4) {
$values = array_map("intval", explode(",", $colval));
$am = 0;
for ($i = 0; $i < count($values); $i++) {
$am += $values[$i];
}
echo "<td>$am</td>";
} else {
echo "<td>$colval</td>";
}
}
$where++;
}
if (($rownumber <= $page*10 && abs($page*10 - $rownumber < 10))) {
echo "<td><a href='Admin_vieworder.php?Id=".$id."' class='btn btn-dark'>View order</a><td>";
}
$rownumber ++;
echo "</tr>\n";
}
echo "</table>\n";
$amountOfRows = $res->num_rows;
if ($amountOfRows == 0) {
echo "No results found";
}
$res->close();
?>
<?php
if (isset($_GET['search'])) {
$s = $_GET['search'];
$s2 = $_GET['searchBy'];
$do = $page - 1;
$change = true;
if ($do <= 0) {
$change = false;
} else if ($do > (ceil(($amountOfRows) / 10))) {
$change = false;
}
if ($change) {
echo '<a class="btn btn-dark" href="Admin_orders.php?search='.$s.'&page='.($page-1).'&searchBy='.$s2.'"><</a>';
}
} else {
$do = $page - 1;
$change = true;
if ($do <= 0) {
$change = false;
} else if ($do > (ceil(($amountOfRows) / 10))) {
$change = false;
}
if ($change) {
echo '<a class="btn btn-dark" href="Admin_orders.php?page='.($page-1).'"><</a>';
}
}
?>
<?php if ($amountOfRows > 0) { echo"<a>\t".$page." of ".(ceil(($amountOfRows) / 10))."\t</a>"; }?>
<?php
if (isset($_GET['search'])) {
$s = $_GET['search'];
$s2 = $_GET['searchBy'];
$do = $page + 1;
$change = true;
if ($do <= 0) {
$change = false;
} else if ($do > (ceil(($amountOfRows) / 10))) {
$change = false;
}
if ($change) {
echo '<a class="btn btn-dark" href="Admin_orders.php?search='.$s.'&page='.($page+1).'&searchBy='.$s2.'">></a>';
}
} else {
$do = $page + 1;
$change = true;
if ($do <= 0) {
$change = false;
} else if ($do > (ceil(($amountOfRows) / 10))) {
$change = false;
}
if ($change) {
echo '<a class="btn btn-dark" href="Admin_orders.php?page='.($page+1).'">></a>';
}
}
?>
<?php echo '<a id="delete" class="btn float-right bg-warning text-light" href="">delete</a>' ?>
</div>
</div>
<script>
var selected = [];
var link = document.getElementById("delete");
var searchBy = "Name";
link.setAttribute("href", "Admin_orders.php");
function checkBoxes(id) {
var check = document.getElementById("check"+id);
if (check.checked) {
selected.push(id);
} else {
var ind = selected.indexOf(id);
if (ind > -1) {
selected.splice(ind, 1);
}
}
if (selected.length > 0) {
link.setAttribute("href", "Admin_orders.php?delete="+selected.join(",")+"");
} else {
link.setAttribute("href", "Admin_orders.php");
}
}
var searchBar = document.getElementById("searchBar");
searchBar.addEventListener("keydown", function (e) {
if (e.keyCode === 13) {
search();
}
});
function search() {
var searchBar = document.getElementById("searchBar");
if (searchBar.value != "") {
if (document.getElementById('amountofproductsRadio').checked) {
searchBy = "amount";
} else if (document.getElementById('tpriceRadio').checked) {
searchBy = "price";
} else if (document.getElementById('addressRadio').checked){
searchBy = "address";
}
if (searchBy == "amount" || searchBy == "price") {
if (isNaN(searchBar.value)) {
alert("Must be a number");
return;
}
}
window.location = "Admin_orders.php?search="+searchBar.value+"&searchBy="+searchBy;
}
}
function show() {
var options = document.getElementById("options");
var text = document.getElementById("searchoptions");
searchoptions
if (options.style.display == "none") {
options.style.display = "block";
text.innerHTML = "Hide search options";
} else {
options.style.display = "none";
text.innerHTML = "Show search options";
}
}
</script>
</div>
<?php
include 'footer.php'
?>
</body>
</html>