-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin_dashboard.php
473 lines (446 loc) · 18 KB
/
admin_dashboard.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
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
<?php
// we will only start the session with session_start() IF the session isn't started yet //
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
?>
<?php
// including the conn.php to establish connection with database //
include "conn.php";
?>
<?php
// Present Date //
$present = date_create();
// Taking the current time //
date_default_timezone_set("Etc/GMT-8");
$now = date("Y-m-d H:i:s");
// Name of the current month //
$month_name = (strftime("%B",time()));
// Variable to store Error Message //
$error = '';
// Bar Chart //
// To count the average value of the reviews //
// Formula: (Total Rating / Total number of product bought) from this month and this year
$REVIEWSTOP5 = "SELECT product_id,
(SUM(review_rating) / COUNT(*)) AS AVGRATING,
COUNT(*) AS QUANTITY FROM review
WHERE MONTH(review_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(review_date) = YEAR(CURRENT_TIMESTAMP())
GROUP BY product_id
ORDER BY AVGRATING ASC LIMIT 5";
$REVIEWSTOP5Q = mysqli_query($db, $REVIEWSTOP5);
// Formula: (Total Rating / Total number of product bought) from this month and this year
// P.S: Repeated due to unknown error causing the graph not showing the value, although able to print it
$REVIEWSTOP5QUAN = "SELECT (SUM(review_rating) / COUNT(*)) AS AVGRATING,
COUNT(*) AS QUANTITY FROM review
WHERE MONTH(review_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(review_date) = YEAR(CURRENT_TIMESTAMP())
GROUP BY product_id
ORDER BY AVGRATING ASC LIMIT 5";
$REVIEWSTOP5QUANQ = mysqli_query($db, $REVIEWSTOP5QUAN);
// Formula: (Total Rating / Total number of product bought) from this month and this year
$REVIEWSTOP5_2 = "SELECT product_id,
(SUM(review_rating) / COUNT(*)) AS AVGRATING,
COUNT(*) AS QUANTITY FROM review
WHERE MONTH(review_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(review_date) = YEAR(CURRENT_TIMESTAMP())
GROUP BY product_id
ORDER BY AVGRATING ASC LIMIT 5";
$REVIEWSTOP5_2Q = mysqli_query($db, $REVIEWSTOP5_2);
// Progress Bar //
// Get the total number of specific product sold within the current month and year
$SPECPRODUCTTOP10 = "SELECT cart_item.product_id, COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1
GROUP BY cart_item.product_id
ORDER BY QUANTITY DESC LIMIT 10";
$SPECPRODUCTTOP10Q = mysqli_query($db, $SPECPRODUCTTOP10);
// Get the total number of all product sold within the current month and year
$ALLPRODUCTSOLD = "SELECT COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1";
$ALLPRODUCTSOLDQ = mysqli_query($db, $ALLPRODUCTSOLD);
// Doughnut Chart //
// Get the total number of specific catetgory sold within the current month and year
$CATEGORYTOP5 = "SELECT category.category_id, category.category_name, cart_item.product_id, COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
INNER JOIN product ON cart_item.product_id = product.product_id
INNER JOIN category ON product.category_id = category.category_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1
GROUP BY category.category_id
ORDER BY QUANTITY DESC LIMIT 5";
$CATEGORYTOP5Q = mysqli_query($db, $CATEGORYTOP5);
// P.S: Repeated due to unknown error although the query can run smoothly at once
// Get the total number of all product sold within the current month and year
$ALLPRODUCTSOLD_2 = "SELECT COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1";
$ALLPRODUCTSOLD_2Q = mysqli_query($db, $ALLPRODUCTSOLD_2);
// Reloading charts function in javascript
// P.S: Repeated due to unknown error although the query can run smoothly at once
// Formula: (Total Rating / Total number of product bought) from this month and this year
$REVIEWSTOP5_3 = "SELECT product_id,
(SUM(review_rating) / COUNT(*)) AS AVGRATING,
COUNT(*) AS QUANTITY FROM review
WHERE MONTH(review_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(review_date) = YEAR(CURRENT_TIMESTAMP())
GROUP BY product_id
ORDER BY AVGRATING ASC LIMIT 5";
$REVIEWSTOP5_3Q = mysqli_query($db, $REVIEWSTOP5_3);
// P.S: Repeated due to unknown error although the query can run smoothly at once
// Formula: (Total Rating / Total number of product bought) from this month and this year
$REVIEWSTOP5QUAN_2 = "SELECT (SUM(review_rating) / COUNT(*)) AS AVGRATING,
COUNT(*) AS QUANTITY FROM review
WHERE MONTH(review_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(review_date) = YEAR(CURRENT_TIMESTAMP())
GROUP BY product_id
ORDER BY AVGRATING ASC LIMIT 5";
$REVIEWSTOP5QUAN_2Q = mysqli_query($db, $REVIEWSTOP5QUAN_2);
// P.S: Repeated due to unknown error although the query can run smoothly at once
// Get the total number of all product sold within the current month and year
$ALLPRODUCTSOLD_3 = "SELECT COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1";
$ALLPRODUCTSOLD_3Q = mysqli_query($db, $ALLPRODUCTSOLD_3);
// P.S: Repeated due to unknown error although the query can run smoothly at once
$CATEGORYTOP5_2 = "SELECT category.category_id, category.category_name, cart_item.product_id, COUNT(*) AS QUANTITY FROM cart
INNER JOIN cart_item ON cart_item.cart_id = cart.cart_id
INNER JOIN product ON cart_item.product_id = product.product_id
INNER JOIN category ON product.category_id = category.category_id
WHERE MONTH(cart.cart_date) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(cart.cart_date) = YEAR(CURRENT_TIMESTAMP()) AND cart.cart_status = 1
GROUP BY category.category_id
ORDER BY QUANTITY DESC LIMIT 5";
$CATEGORYTOP5_2Q = mysqli_query($db, $CATEGORYTOP5_2);
?>
<!-- chart.js script link -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<!-- canvasjs.com doughnut chart script link -->
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div class="widget dashboard-container">
<h3 class="widget-header">Dashboard</h3>
<!-- Based on top 5 reviews -->
<h2 class="widget-header">Top 5 Trends of the Month (<?php echo $month_name; ?>)</h2>
<canvas id="myChartReview" width="400" height="200"></canvas>
<div class="container">
<div class="row">
<?php
$cn = 0;
$n = 0;
while($row = mysqli_fetch_array($REVIEWSTOP5_2Q)) {
// calling out the product names accordingly
$PRODUCTNAMES = "SELECT product_name FROM product WHERE product_id = ".$row['product_id'];
$PRODUCTNAMESQ = mysqli_query($db, $PRODUCTNAMES);
if (mysqli_num_rows($PRODUCTNAMESQ) < 1) {
echo "NULL";
} else {
if ($row = mysqli_fetch_array($PRODUCTNAMESQ)) {
echo '
<div class="col" style="text-overflow: ellipsis; overflow:hidden;">
<svg width="15" height="13">
<rect width="15" height="10" style="fill:';
if ($cn == 0) {
echo 'rgba(255, 99, 132, 0.2)';
} else if ($cn == 1) {
echo 'rgba(54, 162, 235, 0.2)';
} else if ($cn == 2) {
echo 'rgba(255, 206, 86, 0.2)';
} else if ($cn == 3) {
echo 'rgba(75, 192, 192, 0.2)';
} else if ($cn == 4) {
echo 'rgba(153, 102, 255, 0.2)';
}
echo '; stroke-width:1; stroke:';
if ($cn == 0) {
echo 'rgba(255, 99, 132, 1)';
} else if ($cn == 1) {
echo 'rgba(54, 162, 235, 1)';
} else if ($cn == 2) {
echo 'rgba(255, 206, 86, 1)';
} else if ($cn == 3) {
echo 'rgba(75, 192, 192, 1)';
} else if ($cn == 4) {
echo 'rgba(153, 102, 255, 1)';
}
echo ';" />
</svg> '.strtoupper($row["product_name"]).' </div>';
}
}
$cn++;
$n++;
if ($n == 2) {
echo '<div class="w-100"></div>';
$n = 0;
}
}
?>
</div>
</div>
<br>
<br>
<!-- Based on top 10 products -->
<h2 class="widget-header">Top 10 Products of the Month (<?php echo $month_name; ?>)</h2>
<div class="container" style="font-size:13px!important">
<?php
$n = 0;
// first get the total product sold of the current month and year for future calculation
if (mysqli_num_rows($ALLPRODUCTSOLDQ) < 1) {
$totalproduct = 0;
} else {
if ($row = mysqli_fetch_array($ALLPRODUCTSOLDQ)) {
$totalproduct = $row["QUANTITY"];
}
}
if ($totalproduct > 0) {
while($row = mysqli_fetch_array($SPECPRODUCTTOP10Q)) {
// performing calculation on the percentage
$top10percent = number_format((float)(($row["QUANTITY"] / $totalproduct) * 100), 2, '.', '');
// calling out the product names accordingly
$PRODUCTNAMES = "SELECT product_name FROM product WHERE product_id = ".$row['product_id'];
$PRODUCTNAMESQ = mysqli_query($db, $PRODUCTNAMES);
if (mysqli_num_rows($PRODUCTNAMESQ) < 1) {
echo "NULL";
} else {
if ($row = mysqli_fetch_array($PRODUCTNAMESQ)) {
echo '
<div class="row">
<div class="col-3">
<div class="progresstitle" style="text-overflow: ellipsis; overflow:hidden;">'.strtoupper($row["product_name"]).'</div>
</div>
<div class="col-7">
<div class="progress" style="height:100%!important; max-height:20px!important">
<div class="progress-bar" role="progressbar" style="width: '.$top10percent.'%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
<div class="col-1">
'.$top10percent.'%
</div>
</div>
';
}
}
if ($n < mysqli_num_rows($SPECPRODUCTTOP10Q)) {
echo '<br>';
$n++;
}
}
} else {
echo '<div style="color:red;">System Notice: No available data for calculation. </div>';
}
?>
</div>
<br>
<br>
<!-- Based on top 5 category -->
<h2 class="widget-header">Top 5 Category of the Month (<?php echo $month_name; ?>)</h2>
<div class="container" style="font-size:13px!important">
<div id="myChartCategory" style="height: 370px; min-width: 100%;"></div>
</div>
</div>
<script>
var ctx = document.getElementById('myChartReview').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [
<?php
while($row = mysqli_fetch_array($REVIEWSTOP5Q)) {
// calling out the product names accordingly
$PRODUCTNAMES = "SELECT product_name FROM product WHERE product_id = ".$row['product_id'];
$PRODUCTNAMESQ = mysqli_query($db, $PRODUCTNAMES);
// first we check whether the data can be found from the database or note
echo "'";
if (mysqli_num_rows($PRODUCTNAMESQ) < 1) {
echo "NULL";
} else {
if ($row = mysqli_fetch_array($PRODUCTNAMESQ)) {
echo strtoupper($row["product_name"]);
}
}
echo "',";
}
?>
],
datasets: [{
label: ' Average Rating ',
data: [
<?php
while($row = mysqli_fetch_array($REVIEWSTOP5QUANQ)) {
echo "'";
// calling out the value accordinly
$avgrating = number_format((float)$row["AVGRATING"], 2, '.', '');
echo $avgrating;
echo "',";
}
?>
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
<!-- function to reload the bar chart -->
<script>
function reloadBar() {
var ctx = document.getElementById('myChartReview').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [
<?php
while($row = mysqli_fetch_array($REVIEWSTOP5_3Q)) {
// calling out the product names accordingly
$PRODUCTNAMES = "SELECT product_name FROM product WHERE product_id = ".$row['product_id'];
$PRODUCTNAMESQ = mysqli_query($db, $PRODUCTNAMES);
// first we check whether the data can be found from the database or note
echo "'";
if (mysqli_num_rows($PRODUCTNAMESQ) < 1) {
echo "NULL";
} else {
if ($row = mysqli_fetch_array($PRODUCTNAMESQ)) {
echo strtoupper($row["product_name"]);
}
}
echo "',";
}
?>
],
datasets: [{
label: ' Average Rating ',
data: [
<?php
while($row = mysqli_fetch_array($REVIEWSTOP5QUAN_2Q)) {
echo "'";
// calling out the value accordinly
$avgrating = number_format((float)$row["AVGRATING"], 2, '.', '');
echo $avgrating;
echo "',";
}
?>
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
</script>
<!-- Donut Chart Scripts -->
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("myChartCategory", {
animationEnabled: true,
title:{
text: "Product Categories",
horizontalAlign: "left"
},
data: [{
type: "doughnut",
startAngle: 60,
//innerRadius: 60,
indexLabelFontSize: 17,
indexLabel: "{label} - #percent%",
toolTipContent: "<b>{label}:</b> {y} (#percent%)",
dataPoints: [
<?php
// first get the total product sold of the current month and year for future calculation
if (mysqli_num_rows($ALLPRODUCTSOLDQ) < 1) {
$totalproduct = 0;
} else {
if ($row = mysqli_fetch_array($ALLPRODUCTSOLDQ)) {
$totalproduct = $row["QUANTITY"];
}
}
if ($totalproduct > 0) {
while($row = mysqli_fetch_array($CATEGORYTOP5Q)) {
echo '{ y: '.$row["QUANTITY"].', label: "'.strtoupper($row["category_name"]).'" },';
$totalproduct = $totalproduct - $row["QUANTITY"];
}
echo '{ y: '.$totalproduct.', label: "OTHERS" },';
}
?>
]
}]
});
chart.render();
}
</script>
<!-- function to reload the Donut Chart-->
<script>
function reloadDonut() {
var chart = new CanvasJS.Chart("myChartCategory", {
animationEnabled: true,
title:{
text: "Product Categories",
horizontalAlign: "left"
},
data: [{
type: "doughnut",
startAngle: 60,
//innerRadius: 60,
indexLabelFontSize: 17,
indexLabel: "{label} - #percent%",
toolTipContent: "<b>{label}:</b> {y} (#percent%)",
dataPoints: [
<?php
// first get the total product sold of the current month and year for future calculation
if (mysqli_num_rows($ALLPRODUCTSOLD_3Q) < 1) {
$totalproduct = 0;
} else {
if ($row = mysqli_fetch_array($ALLPRODUCTSOLD_3Q)) {
$totalproduct = $row["QUANTITY"];
}
}
if ($totalproduct > 0) {
while($row = mysqli_fetch_array($CATEGORYTOP5_2Q)) {
echo '{ y: '.$row["QUANTITY"].', label: "'.strtoupper($row["category_name"]).'" },';
$totalproduct = $totalproduct - $row["QUANTITY"];
}
echo '{ y: '.$totalproduct.', label: "OTHERS" },';
}
?>
]
}]
});
chart.render();
}
</script>