-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·206 lines (182 loc) · 7.52 KB
/
index.php
File metadata and controls
executable file
·206 lines (182 loc) · 7.52 KB
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
<link href="https://unpkg.com/basscss@8.0.2/css/basscss.min.css" rel="stylesheet">
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$DB = @new mysqli('localhost', 'root', 'root', 'code_for_australia');
if ($DB->connect_error) {
echo "Error: " . $DB->connect_error;
exit();
}
$validationError = false;
$sanitise = function ($data, $length) use ($DB) {
return $output = mysqli_real_escape_string($DB, substr($data, 0, $length));
};
if (isset($_POST['submit'])) {
echo '<div class="center p2">
<div class="max-width-3 mx-auto left-align">';
$required = '';
if (isset($_POST['namefirst']) && !empty($_POST['namefirst'])) {
$namefirst = $sanitise($_POST['namefirst'], 32);
} else {
echo '<strong>Given name(s)</strong> field is required<br/>';
$validationError = true;
}
if (isset($_POST['namelast']) && !empty($_POST['namelast'])) {
$namelast = $sanitise($_POST['namelast'], 32);
} else {
echo '<strong>Surname</strong> field is required<br/>';
$validationError = true;
}
if (isset($_POST['email']) && !empty($_POST['email'])) {
$email = $sanitise($_POST['email'], 64);
} else {
echo '<strong>Email</strong> field is required<br/>';
$validationError = true;
}
if (isset($_POST['age']) && !empty($_POST['age'])) {
$age = (int) $sanitise($_POST['age'], 3);
} else {
echo '<strong>Age</strong> field is required<br/>';
$validationError = true;
}
if (isset($_POST['fellowship']) && !empty($_POST['fellowship'])) {
$fellowship = $sanitise($_POST['fellowship'], 16);
} else {
echo '<strong>Fellowship</strong> field is required<br/>';
$validationError = true;
}
if (isset($_POST['about']) && !empty($_POST['about'])) {
$about = $sanitise($_POST['about'], 2048);
} else {
echo '<strong>About</strong> field is required<br/>';
$validationError = true;
}
if ($validationError) {
echo "<br/>Form submission was invalid, please correct the errors above.";
// header('Location: index.php');
// exit();
} else {
$timestamp = $sanitise(date("l, F n, Y g:i A", time()), 64);
$id = $sanitise(hash('sha256', $timestamp), 24);
if ($fellowship === 'uncategorised') {
$insert = $DB->query("INSERT INTO fellows (id, isActive, age, namefirst, namelast, email, about, registered)
VALUES ('$id', 1, $age, '$namefirst', '$namelast', '$email', '$about', '$timestamp')");
} else {
$insert = $DB->query("INSERT INTO fellows (id, isActive, age, fellowship, namefirst, namelast, email, about, registered)
VALUES ('$id', 1, $age, '$fellowship', '$namefirst', '$namelast', '$email', '$about', '$timestamp')");
}
echo "New fellow <strong>" . $namefirst . $namelast . "</strong> successfully added to register under the <strong>" . $fellowship . "</strong> fellowship category";
}
echo '</div></div><hr/>';
}
$result = $DB->query("SELECT namefirst, namelast, age, fellowship
FROM fellows
GROUP BY fellowship, age, namefirst, namelast");
$fellowsGrouped = [];
foreach ($result as $row) {
$fellowship = $row['fellowship'];
if ($fellowship === null) {
$fellowship = "Uncategorised";
}
if (!isset($fellowsGrouped[$fellowship])) {
$fellowsGrouped[$fellowship] = [];
}
$details = [];
$details['name'] = $row['namefirst'] . " " . $row['namelast'];
$details['age'] = $row['age'];
array_push($fellowsGrouped[$fellowship], $details);
}
$youngestAge = 200;
$oldestAge = 0;
?>
<div class="center p2">
<div class="max-width-3 mx-auto left-align">
<h1>Fellowship Register</h1>
<?php
foreach ($fellowsGrouped as $fellowship => $fellows) {
$average = array_sum(array_column($fellows, 'age')) / count($fellows);
echo '<hr/><div class="flex flex-wrap">
<p class="col-6">Fellowship category name: <strong>' . $fellowship . '</strong></p>
<p class="col-6 right-align">Average fellow age for this category: <strong>' . $average . '</strong></p>
</div>';
echo '<p class="mb0">Fellows:</p>
<div class="flex flex-wrap mb2 pb2 mxn2">';
foreach ($fellows as $fellow) {
echo '<div class="col-4 p2 border-box">
<div class="p2 border">
<p>Name: ' . $fellow['name'] . '<br/>
Age: ' . $fellow['age'] . '</p>';
if ($fellow['age'] < $youngestAge) {
$youngestAge = $fellow['age'];
$youngestFellow = $fellow;
$youngestFellow['fellowship'] = $fellowship;
}
if ($fellow['age'] > $oldestAge) {
$oldestAge = $fellow['age'];
$oldestFellow = $fellow;
$oldestFellow['fellowship'] = $fellowship;
}
echo "</div></div>";
}
echo "</div>";
}
echo '
</div></div>
<hr/>
<div class="center p2">
<div class="max-width-3 mx-auto left-align">
<h2>Youngest and Oldest</h2>
<div class="flex flex-wrap mxn2">
<div class="col-6 px2 border-box">
<h4>Youngest fellow:</h4>
<div class="p2 border">
<p>Name: ' . $youngestFellow['name'] . '<br/>
Age: ' . $youngestFellow['age'] . '<br/><br/>
Fellowship category: ' . $youngestFellow['fellowship'] . '</p>
</div>
</div>
<div class="col-6 px2 border-box">
<h4>Oldest fellow:</h4>
<div class="p2 border">
<p>Name: ' . $oldestFellow['name'] . '<br/>
Age: ' . $oldestFellow['age'] . '<br/><br/>
Fellowship category: ' . $oldestFellow['fellowship'] . '</p>
</div>
</div>
</div>
</div>
</div>
<hr class="mt4"/>
<div class="center p2">
<div class="max-width-3 mx-auto left-align">
<form method="post" action="">
<h2 class="mb0">Add a fellow</h2>
<h5 class="mt1">(all fields required)</h5>
<div class="flex flex-wrap mxn2 left-align">
<div class="col-6 flex flex-column p2 border-box">
<input type="text" name="namefirst" placeholder="Given name(s)" class="mb2 p1"/>
<input type="text" name="namelast" placeholder="Surname name" class="mb2 p1"/>
<input type="email" name="email" placeholder="Email" class="mb2 p1"/>
<input type="number" name="age" min="13" max="150" placeholder="Age" class="mb2 p1"/>
<select name="fellowship">
<option value="BLA">BLA</option>
<option value="CityZap">CityZap</option>
<option value="Relwp">Relwp</option>
<option value="VicWays">VicWays</option>
<option value="ZESE">ZESE</option>
<option value="uncategorised">Uncategorised</option>
</select>
</div>
<div class="col-6 p2 border-box">
<label>About:</label>
<textarea rows="16" maxlength="2048" class="col-12" name="about"></textarea>
</div>
</div>
<div class="center mb3">
<button name="submit" class="col-6 p2 mx-auto">Add fellow</button>
</div>
</form>
</div>
</div>';
$result->close();
$DB->close();