-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder1.php
276 lines (267 loc) · 7.26 KB
/
order1.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
<?php
require_once('header.php');
$sql = "SELECT id, name
FROM users";
$result = mysqli_query($conn, $sql);
$sql2 = "SELECT id, description
FROM order_status";
$result2 = mysqli_query($conn, $sql2);
$sql3 = "SELECT id, title
FROM gigs
ORDER BY id ASC";
$result3 = mysqli_query($conn, $sql3);
$sql4 = "SELECT id, title
FROM gigs
ORDER BY id ASC";
$result4 = mysqli_query($conn, $sql4);
?>
<div class="main">
<h3> Get buyers based on the seller and order status (JOIN) </h3>
<form action="" method="GET">
<div class="row">
<div class="col-25">
<label for="seller"> Select Seller: </label>
</div>
<div class="col-75">
<select name="seller">
<?php
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="status"> Select Status: </label>
</div>
<div class="col-75">
<select name="status">
<?php
if (mysqli_num_rows($result2) > 0)
{
while ($row2 = mysqli_fetch_assoc($result2))
{
echo "<option value=" . $row2['id'] . ">" . $row2['description'] . "</option>";
}
}
?>
</select>
</div>
</div>
<br>
<div class="row">
<input type="submit" name="query1" value="Run" />
</div>
</form>
<br>
<?php
if (isset($_GET['query1']))
{
$seller = $_GET['seller'];
$status = $_GET['status'];
$sql = "SELECT o.id, u.name as buyer, g.title as gig, s.description as status
FROM order_status s
JOIN orders o ON s.id=o.status
JOIN gigs g ON o.gig=g.id
JOIN users u ON u.id=o.buyer
WHERE g.user_id=$seller AND s.id=$status
ORDER BY o.id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr><th> Buyer
</th><th> Gig
</th><th> Status </th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['buyer'] .
"</td><td>" . $row['gig'] .
"</td><td>" . $row['status'] . "</td></tr>";
}
echo "</table>";
}
else
{
echo "<h2>0 results</h2>";
}
}
?>
</div>
<br>
<div class="main">
<h3> Get sellers and their first skill (JOIN) </h3>
<form action="" method="GET">
<div class="row">
<input type="submit" name="query2" value="Run" />
</div>
</form>
<br>
<?php
if (isset($_GET['query2']))
{
$sql = "SELECT *
FROM users u, skills s
WHERE u.skill_1 = s.id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "</th><th> Name
</th><th> Email
</th><th> 1st Skill </th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['name'] .
"</td><td>" . $row['email'] .
"</td><td>" . $row['skill'] . "</td></tr>";
}
echo "</table>";
}
else
{
echo "<h2>0 results </h2>";
}
}
?>
</div>
<br>
<div class="main">
<h3> Get total orders and revenue of a selected gig (AGGREGATION) </h3>
<form action="" method="GET">
<div class="row">
<div class="col-25">
<label for="id"> Select Gig: </label>
</div>
<div class="col-75">
<select name="id">
<?php
if (mysqli_num_rows($result3) > 0)
{
while ($row3 = mysqli_fetch_assoc($result3))
{
echo "<option value=" . $row3['id'] . ">" . $row3['title'] . "</option>";
}
}
?>
</select>
</div>
</div>
<br>
<div class="row">
<input type="submit" name="query3" value="Run"/>
</div>
</form>
<br>
<?php
if (isset($_GET['query3']))
{
$id = $_GET['id'];
$sql = "SELECT g.id, g.title, g.price, COUNT(o.gig) as order_count, SUM(g.price) as revenue
FROM gigs g
JOIN orders o ON g.id=o.gig
WHERE g.id=$id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
$rev = 0;
echo "<table>";
echo "<tr><th> ID
</th><th> Title
</th><th> Price
</th><th> Total Orders
</th><th> Revenue </th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
if ($row['revenue'])
{
$rev = $row['revenue'];
}
echo "<tr><td>" . $row['id'] .
"</td><td>" . $row['title'] .
"</td><td>$" . $row['price'] .
"</td><td>" . $row['order_count'] .
"</td><td>$" . $rev . "</td></tr>";
}
echo "</table>";
}
else
{
echo "<h2>0 results</h2>";
}
}
?>
</div>
<br>
<div class="main">
<h3> Get users and their total orders of a selected gig (AGGREGATION) </h3>
<form action="" method="GET">
<div class="row">
<div class="col-25">
<label for="id"> Select Gig: </label>
</div>
<div class="col-75">
<select name="id">
<?php
if (mysqli_num_rows($result4) > 0)
{
while ($row4 = mysqli_fetch_assoc($result4))
{
echo "<option value=" . $row4['id'] . ">" . $row4['title'] . "</option>";
}
}
?>
</select>
</div>
</div>
<br>
<div class="row">
<input type="submit" name="query4" value="Run"/>
</div>
</form>
<br>
<?php
if (isset($_GET['query4']))
{
$id = $_GET['id'];
$sql = "SELECT DISTINCT u.id, u.name, u.email, g.title, COUNT(o.id) as total_count
FROM users u
INNER JOIN orders o ON u.id=o.buyer
INNER JOIN gigs g ON o.gig=g.id
WHERE g.id=$id
GROUP BY u.id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr><th> ID
</th><th> Name
</th><th> Email
</th><th> Gig
</th><th> Total Orders </th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['id'] .
"</td><td>" . $row['name'] .
"</td><td>" . $row['email'] .
"</td><td>" . $row['title'] .
"</td><td>" . $row['total_count'] . "</td></tr>";
}
echo "</table>";
}
else
{
echo "<h2>0 results </h2>";
}
}
?>
</div>
<?php
require_once('footer.php');
?>