Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/projekt/classes/
card.php
collection.php
filtersort.php
lineup.php
user.php
collection.php
166 lines UTF-8 Windows (CRLF)
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
<?php
include("./incl/default.php");
class Collection
{
private $db;
public function __construct($dbconn)
{
$this->db = $dbconn;
//Slippa skicka $dbconn varje gång du kallar en metod
}
public function addtocollection($playerid, $userid)
{
try {
$sql = "INSERT INTO owned_cards (user_id, player_id)
VALUES (?, ?)";
$stmt = $this->db->prepare($sql);
$data = array($userid, $playerid);
$stmt->execute($data);
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function removefromcollection($userid, $playerid)
{
try {
$sql = "DELETE FROM startelva
WHERE userid = ? AND
owned_id = ?";
$stmt = $this->db->prepare($sql);
$data = array($userid, $playerid);
$stmt->execute($data);
//ta bort från din startelva
$sql = "DELETE FROM owned_cards WHERE user_id=? AND id = ?";
$stmt = $this->db->prepare($sql);
$data = array($userid, $playerid);
$stmt->execute($data);
//ta bort från din samling
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function showownedplayers($userid)
{
try {
$sql = "SELECT owned_cards.id AS playerid, players.name AS name
FROM owned_cards
RIGHT JOIN players ON owned_cards.player_id = players.id
WHERE owned_cards.user_id = ?";
$stmt = $this->db->prepare($sql);
$data = array($userid);
$stmt->execute($data);
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<input type='checkbox' name='" . htmlentities($res['playerid']) . "'>" . htmlentities($res['name']) . "</option> <br>";
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function show_notownedplayers($userid)
{
try {
$sql = "SELECT player_id
FROM owned_cards
WHERE user_id = ?";
$stmt = $this->db->prepare($sql);
$data = array($userid);
$stmt->execute($data);
$ownedplayers = [];
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ownedplayers[] = $res['player_id'];
}
$sql = "SELECT id, name
FROM players";
$stmt = $this->db->prepare($sql);
$stmt->execute();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
if (!in_array($res['id'], $ownedplayers)) {
echo "<input type='checkbox' name='" . htmlentities($res['id']) . "'>" . htmlentities($res['name']) . "</option> <br>";
}
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function leaderboard()
{
try {
$sql = "SELECT id, name FROM projektusers";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$users = [];
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$users[] = [
'id' => $res['id'],
'name' => $res['name']
];
}
$leaderboard = [];
foreach ($users as $user) {
$sql = "SELECT players.rating AS rating, players.name AS name
FROM owned_cards
RIGHT JOIN players ON owned_cards.player_id = players.id
WHERE owned_cards.user_id = ?";
$stmt = $this->db->prepare($sql);
$stmt->execute([$user['id']]);
$highestrating = 0;
$antalspelare = 0;
$highestname = '';
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$antalspelare++;
if ($res['rating'] > $highestrating) {
$highestrating = $res['rating'];
$highestname = $res['name'];
}
}
$sql = "SELECT players.rating AS rating
FROM startelva
RIGHT JOIN owned_cards ON startelva.owned_id = owned_cards.id
RIGHT JOIN players ON owned_cards.player_id = players.id
WHERE startelva.userid = ?";
$stmt = $this->db->prepare($sql);
$stmt->execute([$user['id']]);
$totalRating = 0;
$count = 0;
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$totalRating += $res['rating'];
$count++;
}
$average = $count > 0 ? round($totalRating / $count) : 0;
$leaderboard[] = [
'id' => $user['id'],
'name' => $user['name'],
'antalspelare' => $antalspelare,
'highestname' => $highestname,
'average' => $average
];
}
// Sortera efter average (högst först)
usort($leaderboard, function ($a, $b) {
return $b['average'] - $a['average'];
});
// Skriva ut HTML-raderna
foreach ($leaderboard as $entry) {
echo "<tr onclick=\"lineup(" . htmlentities($entry['id']) . ", '" . htmlentities($entry['name']) . "')\">
<td>" . htmlentities($entry['name']) . "</td>
<td>" . htmlentities($entry['antalspelare']) . "</td>
<td>" . htmlentities($entry['highestname']) . "</td>
<td>" . htmlentities($entry['average']) . "</td>
</tr>";
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
}