Webbserverprogrammering 1

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)
<?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();
        }
    }
}