site-accueil-insa/classes/dao.php

66 lines
1.8 KiB
PHP
Raw Normal View History

2018-06-15 09:33:29 +02:00
<?php
2019-05-16 07:28:02 +02:00
class Dao
{
2018-06-15 09:33:29 +02:00
private $conn;
2019-05-16 07:28:02 +02:00
private $debug = true;
2018-06-15 11:12:55 +02:00
2018-06-15 09:33:29 +02:00
public function __construct($path_to_password)
{
2019-05-16 07:28:02 +02:00
if ($this->debug) {
$username = 'phpmyadmin';
$password = $this->read_password($path_to_password);
$dsn = 'mysql:dbname=phpmyadmin;host=127.0.0.1';
} else {
$username = 'accueil_insa';
$password = $this->read_password($path_to_password);
$dsn = 'mysql:dbname=accueil_insa;host=127.0.0.1';
}
2018-06-15 09:33:29 +02:00
try {
$this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
2019-05-16 07:28:02 +02:00
} catch (PDOException $e) {
echo $e;
2018-06-15 09:33:29 +02:00
}
}
2019-05-16 07:28:02 +02:00
private function read_password($path_to_password)
{
if ($this->debug)
$real_path = $path_to_password . "includes/.htpassdb_debug";
else
$real_path = $path_to_password . "includes/.htpassdb";
$file = fopen($real_path, "r") or die("Unable to open file!");;
2018-06-15 09:33:29 +02:00
$password = fgets($file);
fclose($file);
return $password;
}
2019-05-16 07:28:02 +02:00
public function get_score_team($team)
{
$sql = 'SELECT text, points FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
return $cursor->fetchAll(PDO::FETCH_ASSOC);
2018-06-15 09:33:29 +02:00
}
2019-05-16 07:28:02 +02:00
public function save_scores($scores_json, $team)
{
$sql = 'DELETE FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
$array = json_decode($scores_json)->array;
foreach ($array as $value) {
$sql = 'INSERT INTO scores (text, points, team) VALUES (?, ?, ?)';
2018-06-15 09:33:29 +02:00
$cursor = $this->conn->prepare($sql);
2019-05-16 07:28:02 +02:00
$cursor->execute([$value->text, $value->points, $team]);
2018-06-15 09:33:29 +02:00
}
}
}