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
card.php
94 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
<?php
include("./incl/default.php");
class Card
{
private $db;
public function __construct($dbconn)
{
$this->db = $dbconn;
//Slippa skicka $dbconn varje gång du kallar en metod
}
public function createcard($name, $age, $nation, $position, $rating, $pace, $shooting, $passing, $dribbling, $defending, $physical, $addTocollection, $userid)
{
try {
$sql = "SELECT name
FROM players";
$stmt = $this->db->prepare($sql);
$stmt->execute();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($res['name'] == $name) {
die('Spelaren finns redan');
}
}
$sql = "INSERT INTO players (name, age, nation, position, rating, pace, shooting, passing, dribbling, defending, physical)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $this->db->prepare($sql);
$data = array($name, $age, $nation, $position, $rating, $pace, $shooting, $passing, $dribbling, $defending, $physical);
$stmt->execute($data);
$last_id = $this->db->lastInsertId();
if ($addTocollection == true) {
$this->addtocollection($last_id, $userid);
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
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 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();
}
}
}