From 1e1635ebb4c3593b33589cb5da25f0cd818080ac Mon Sep 17 00:00:00 2001 From: William Date: Wed, 26 Jan 2022 14:38:22 +0100 Subject: [PATCH] Add simulator --- app/model/Simulator.php | 33 +++++++++++++++++++++++++++++++++ public/simulator.php | 9 ++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/model/Simulator.php diff --git a/app/model/Simulator.php b/app/model/Simulator.php new file mode 100644 index 0000000..b4d63f1 --- /dev/null +++ b/app/model/Simulator.php @@ -0,0 +1,33 @@ +dbh = $database->conn; + } + + // Return true if team exists, false if not + public function insert(string $cardnumber): bool + { + $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?'); + $sth->execute([$cardnumber]); + + $row = $sth->fetch(PDO::FETCH_ASSOC); + if ($row) + { + // Team exists, insert into time table + $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); + $sth->execute([$row['LagID']]); + return TRUE; + } + // Team does not exist, lets create it + $sth = $this->dbh->prepare( + "INSERT INTO `lagtabell` (`LagID`, `LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`, `Bestetid`) VALUES (NULL, 'NN', 'NN', ?, 'NN', '0', '0', '0', now())" + ); + $sth->execute([$cardnumber]); + return FALSE; + } +} \ No newline at end of file diff --git a/public/simulator.php b/public/simulator.php index c0b058b..f54348e 100644 --- a/public/simulator.php +++ b/public/simulator.php @@ -7,7 +7,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $app->session->flash('Kortnummer kan ikke være tom!', 'danger'); } else { - $app->session->flash("Kortnummer: \"{$_POST['cardnumber']}\" er ikke blitt lastet opp men burde bli implementert snart!", 'success'); + $model = $app->model('Simulator'); + + if ($model->insert($_POST['cardnumber'])) + { + $app->session->flash("Lag funnet for \"{$_POST['cardnumber']}\"", "success"); + } else { + $app->session->flash("Opprettet lag for \"{$_POST['cardnumber']}\""); + } } }