-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
188 lines (156 loc) · 6.39 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width" />
<title>Meeting</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<?php
// Fetching data from the reserved json file and decoding to json format
$jsonobj = file_get_contents('users.json');
$users = json_decode($jsonobj, true);
// Creating a Person object, with a constructor function
class Person
{
// Static class property, for Person instances
public static $instances = [];
// Constructor functions, runs on instance creation, with perameters.
function __construct($userid, $username, $birth, $gender, $postcode)
{
$this->id = $userid;
$this->name = $username;
$this->birth = $birth;
$this->gender = $gender;
$this->postcode = $postcode;
array_push(Person::$instances, $this);
}
// Static metode to find the current user
static function GetUser($id)
{
foreach (Person::$instances as $user) {
if ($user->id == $id) {
return $user;
}
}
}
// Methode for finded mateces of each Person, saved on the matches prop, for the Person
function FindMatches()
{
$isMatchingArray = [];
foreach (Person::$instances as $user) {
if ($user->id !== $this->id) {
$diffrenceDate = date_diff(date_create($this->birth), date_create($user->birth));
$daysDiff = $diffrenceDate->format("%a") / 365.25;
if ($daysDiff <= 2 && $user->gender !== $this->gender) {
array_push($isMatchingArray, $user);
}
}
}
$this->matches = $isMatchingArray;
}
}
// Creating users from json formated data
foreach ($users as $user) {
new Person(
$user['userId'],
$user['name'],
$user['birth'],
$user['gender'],
$user['postcode']
);
}
?>
<?php include_once("components/header.php") ?>
<!-- HTML Documuent -->
<section class="mx-auto mt-4 px-40">
<h1 class="text-6xl font-bold text-blue-600 my-6">Online Users</h1>
<div class="grid grid-rows-3 grid-flow-col gap-4">
<!-- Rendering "online users", every user in json file -->
<?php foreach (Person::$instances as $person) {
echo "
<a href='?id={$person->id}' class='bg-gray-200 p-2 hover:bg-blue-400'>
<div class='text-2xl font-bold'>{$person->name}</div>
<div>{$person->postcode}</div>
<div>{$person->birth}</div>
<div>{$person->gender}</div>
</a>
";
}
?>
</div>
</section>
<section class="mx-auto mt-4 px-40">
<?php
// Check if we have an current user, and find it from the Persons object static methode
// Also finding the matches for the user
if (!empty($_GET['id'])) {
$currentUser = Person::GetUser($_GET['id']);
$currentUser->FindMatches();
?>
<h2 class="text-4xl font-bold text-blue-600 my-6">Selected User</h2>
<!-- Rendering the current users information, and adding number of matches -->
<table class="table-auto w-1/5">
<thead>
<tr class="text-blue-600 text-left">
<th>Property</th>
<th>Person</th>
</tr>
</thead>
<?php echo "<tr class='bg-blue-200'>
<td>Name</td>
<td>{$currentUser->name}</td>
</tr>
<tr>
<td>Gender</td>
<td>{$currentUser->gender}</td>
</tr>
<tr class='bg-blue-200'>
<td>Postal code</td>
<td>{$currentUser->postcode}</td>
</tr>
<tr >
<td>Birth</td>
<td>{$currentUser->birth}</td>
</tr>
<tr class='bg-blue-200'>
<td>Matches</td>
<td>" . count($currentUser->matches) . "</td>
</tr>";
?>
</table>
<section class="mx-auto mt-4 px-40">
<div class="flex p-4 flex-grow w-full max-w-screen-xl justify-between">
<table class="table-auto w-full">
<thead>
<tr class="text-blue-600 text-left">
<th>Name</th>
<th>Gender</th>
<th>Postal code</th>
<th>Birthday</th>
</tr>
</thead>
<?php
// Rendering matches for the current user
foreach ($currentUser->matches as $match) {
echo "<tr class='hover:bg-gray-400 py-4'>
<td>{$match->name}</td>
<td>{$match->gender}</td>
<td>{$match->postcode}</td>
<td>{$match->birth}</td>
</tr>";
}
?>
</table>
</div>
</section>
<?php
} // Ending parentheses of if
?>
</section>
</body>
</html>