forked from Xinyi-Lai/CS411_Geniuses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_db.php
executable file
·152 lines (121 loc) · 6.35 KB
/
dashboard_db.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
<?php
include_once "db_functions.php";
if (isset($_REQUEST['option'])) {
if ($_REQUEST['option'] == 'users') {
// the array to be returned
$arr = array();
$arrCampus = array();
$arrMajor = array();
$arrYear = array();
$conn = connectDB();
$sql = "SELECT Campus, COUNT(*) AS cntCampus FROM Users GROUP BY Campus";
$result = $conn->query($sql);
if ($result) {
while ( $row = mysqli_fetch_assoc($result) ) {
$arrCampus[$row['Campus']] = $row['cntCampus'];
}
} else {
$msg = "Error grouping Users by Campus" . $sql . "<br>" . $conn->error;
}
$sql = "SELECT Major, COUNT(*) AS cntMajor FROM Users GROUP BY Major";
$result = $conn->query($sql);
if ($result) {
while ( $row = mysqli_fetch_assoc($result) ) {
$arrMajor[$row['Major']] = $row['cntMajor'];
}
} else {
$msg = "Error grouping Users by Major" . $sql . "<br>" . $conn->error;
}
$sql = "SELECT Year, COUNT(*) AS cntYear FROM Users GROUP BY Year";
$result = $conn->query($sql);
if ($result) {
while ( $row = mysqli_fetch_assoc($result) ) {
$arrYear[$row['Year']] = $row['cntYear'];
}
} else {
$msg = "Error grouping Users by Year" . $sql . "<br>" . $conn->error;
}
$arr['Campus'] = $arrCampus;
$arr['Major'] = $arrMajor;
$arr['Year'] = $arrYear;
$json=urldecode(json_encode($arr)) ;
echo $json;
$conn->close();
}
else if ($_REQUEST['option'] == 'items') {
// the array to be returned
$arr = array();
$arrSales = array();
$arrRequests = array();
$arrTrans = array();
$conn = connectDB();
// need a full outer join of Sales, Requests, Trans after grouping by Tag
// but MySQL does not support full outer join, so we use left join UNION right join
$sql = "CREATE OR REPLACE VIEW SR_leftJoin AS
(SELECT tmp1.Tag AS Tag, tmp1.cntSales AS cntSales, tmp2.cntRequests AS cntRequests FROM
(SELECT Tag, COUNT(*) AS cntSales FROM Sales GROUP BY Tag) tmp1 LEFT JOIN
(SELECT Tag, COUNT(*) AS cntRequests FROM Requests GROUP BY Tag) tmp2 ON tmp1.Tag=tmp2.Tag );";
$result = $conn->query($sql);
$sql = "CREATE OR REPLACE VIEW SR_rightJoin AS
(SELECT tmp2.Tag AS Tag, tmp1.cntSales AS cntSales, tmp2.cntRequests AS cntRequests FROM
(SELECT Tag, COUNT(*) AS cntSales FROM Sales GROUP BY Tag) tmp1 RIGHT JOIN
(SELECT Tag, COUNT(*) AS cntRequests FROM Requests GROUP BY Tag) tmp2 ON tmp1.Tag=tmp2.Tag );";
$result = $conn->query($sql);
$sql = "CREATE OR REPLACE VIEW SR_fullJoin AS
(SELECT * FROM (SELECT * FROM SR_leftJoin UNION SELECT * FROM SR_rightJoin) tmp );";
$result = $conn->query($sql);
$sql = "CREATE OR REPLACE VIEW SRT_leftJoin AS
(SELECT SR_fullJoin.Tag AS Tag, SR_fullJoin.cntSales AS cntSales, SR_fullJoin.cntRequests AS cntRequests, tmp3.cntTrans AS cntTrans FROM
SR_fullJoin LEFT JOIN
(SELECT Tag, COUNT(*) AS cntTrans FROM Transactions GROUP BY Tag) tmp3 ON SR_fullJoin.Tag=tmp3.Tag );";
$result = $conn->query($sql);
$sql = "CREATE OR REPLACE VIEW SRT_rightJoin AS
(SELECT tmp3.Tag AS Tag, SR_fullJoin.cntSales AS cntSales, SR_fullJoin.cntRequests AS cntRequests, tmp3.cntTrans AS cntTrans FROM
SR_fullJoin RIGHT JOIN
(SELECT Tag, COUNT(*) AS cntTrans FROM Transactions GROUP BY Tag) tmp3 ON SR_fullJoin.Tag=tmp3.Tag );";
$result = $conn->query($sql);
$sql = "SELECT * FROM (SELECT * FROM SRT_leftJoin UNION SELECT * FROM SRT_rightJoin) tmp";
$result = $conn->query($sql);
if ($result) {
while ( $row = mysqli_fetch_assoc($result) ) {
$arrSales[$row['Tag']] = $row['cntSales'] == null ? 0 : $row['cntSales'];
$arrRequests[$row['Tag']] = $row['cntRequests'] == null ? 0 : $row['cntRequests'];
$arrTrans[$row['Tag']] = $row['cntTrans'] == null ? 0 : $row['cntTrans'];
}
} else {
$msg = "Error full join after grouping by tag" . $sql . "<br>" . $conn->error;
}
$arr['Sales'] = $arrSales;
$arr['Requests'] = $arrRequests;
$arr['Trans'] = $arrTrans;
$json=urldecode(json_encode($arr)) ;
echo $json;
$conn->close();
} elseif($_REQUEST['option'] == 'price'){
// the array to be returned
$arr = array();
$arrMax = array();
$arrAvg = array();
$arrMin = array();
$conn = connectDB();
$sql = "SELECT Tag, max(IntendedPrice) as MaxPrice, min(IntendedPrice) as MinPrice, avg(IntendedPrice) as AvgPrice FROM Sales GROUP BY Tag";
$result = $conn->query($sql);
if ($result) {
while ( $row = mysqli_fetch_assoc($result) ) {
$arrMax[$row['Tag']] = $row['MaxPrice'] == null ? 0 : $row['MaxPrice'];
$arrAvg[$row['Tag']] = $row['AvgPrice'] == null ? 0 : $row['AvgPrice'];
$arrMin[$row['Tag']] = $row['MinPrice'] == null ? 0 : $row['MinPrice'];
}
} else {
$msg = "Error full join after grouping by tag" . $sql . "<br>" . $conn->error;
}
$arr['Max'] = $arrMax;
$arr['Avg'] = $arrAvg;
$arr['Min'] = $arrMin;
$json=urldecode(json_encode($arr)) ;
echo $json;
$conn->close();
}
}
// echo "<script>console.log('$msg');</script>";
?>