New DB based GuestBook
This commit is contained in:
parent
15d3507b04
commit
70da3e66fd
@ -3,5 +3,10 @@
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
]
|
||||
],
|
||||
"settings": {
|
||||
"cSpell.words": [
|
||||
"benutzer"
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
<head>
|
||||
<?php include_once "../../base/meta.php" ?>
|
||||
<title>Counter</title>
|
||||
<title>Functions</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
96
tasks/functions2/index.php
Normal file
96
tasks/functions2/index.php
Normal file
@ -0,0 +1,96 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<?php include_once "../../base/meta.php" ?>
|
||||
<title>Functions 2</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<?php
|
||||
include_once "../../base/settings.php";
|
||||
include_once "../../base/headers.php";
|
||||
Headers::html();
|
||||
|
||||
include_once "../../base/database.php";
|
||||
|
||||
$db = DB::openConnection();
|
||||
|
||||
function getFirmenAnzahl($land, $db)
|
||||
{
|
||||
$query = "SELECT COUNT(*) AS Anzahl FROM Lieferanten WHERE Land = :LAND";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(":LAND", $land);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch(PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
function getArtikelname($artikelnr, $db)
|
||||
{
|
||||
$query = "SELECT Artikelname FROM Artikel WHERE ArtikelNr = :NR";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(":NR", $artikelnr);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch(PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
function getArtikelAnzahl($firma, $db)
|
||||
{
|
||||
$query = "SELECT COUNT(*) FROM Artikel AS A, Lieferanten AS L WHERE A.LieferantenNr = L.LieferantenNr AND L.Firma = :FIRMA";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(":FIRMA", $firma);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch(PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
function updPercArticle($firma, $perc, $db)
|
||||
{
|
||||
$query = "
|
||||
UPDATE
|
||||
Artikel AS A, Lieferanten AS L
|
||||
SET
|
||||
A.Einzelpreis = A.Einzelpreis * :PERC
|
||||
WHERE
|
||||
A.LieferantenNr = L.LieferantenNr AND
|
||||
L.Firma = :FIRMA";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(":FIRMA", $firma);
|
||||
$stmt->bindValue(":PERC", 1 + $perc / 100);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
$anz_de = getFirmenAnzahl("Deutschland", $db);
|
||||
$anz_fr = getFirmenAnzahl("Frankreich", $db);
|
||||
$name = getArtikelname(1, $db);
|
||||
$anz_exotic = getArtikelAnzahl("Exotic Liquids", $db);
|
||||
|
||||
echo "<p>Es sind $anz_de Firmen in Deutschland ansässig.</p>";
|
||||
echo "<p>Es sind $anz_fr Firmen in Frankreich ansässig.</p>";
|
||||
|
||||
echo "<p>$name hat Art.Nr. 1</p>";
|
||||
|
||||
echo "<p>Exotic Liquids liefert $anz_exotic Artikel</p>";
|
||||
|
||||
echo "<p>Before price hike</p>";
|
||||
$query = "SELECT A.Einzelpreis FROM Artikel AS A, Lieferanten AS L WHERE A.LieferantenNr = L.LieferantenNr AND L.Firma = 'Tokyo Traders'";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->execute();
|
||||
echo "<pre>";
|
||||
var_dump($stmt->fetchAll());
|
||||
echo "</pre>";
|
||||
|
||||
updPercArticle("Tokyo Traders", 10, $db);
|
||||
|
||||
echo "<p>After price hike</p>";
|
||||
$query = "SELECT A.Einzelpreis FROM Artikel AS A, Lieferanten AS L WHERE A.LieferantenNr = L.LieferantenNr AND L.Firma = 'Tokyo Traders'";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->execute();
|
||||
echo "<pre>";
|
||||
var_dump($stmt->fetchAll());
|
||||
echo "</pre>";
|
||||
|
||||
DB::closeConnection($db);
|
||||
?>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
45
tasks/guestBookDB/actions/comment.php
Normal file
45
tasks/guestBookDB/actions/comment.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
include_once "../../../base/settings.php";
|
||||
include_once "../../../base/headers.php";
|
||||
|
||||
session_start();
|
||||
$_SESSION["error"] = [];
|
||||
|
||||
if (!isset($_POST["title"])) {
|
||||
array_push($_SESSION["error"], "Title was not among the data sent.");
|
||||
}
|
||||
if (!isset($_POST["name"])) {
|
||||
array_push($_SESSION["error"], "Name was not among the data sent.");
|
||||
}
|
||||
if (!isset($_POST["comment"])) {
|
||||
array_push($_SESSION["error"], "Comment was not among the data sent.");
|
||||
}
|
||||
|
||||
$title = trim($_POST["title"]);
|
||||
$name = trim($_POST["name"]);
|
||||
$comment = substr(trim($_POST["comment"]), 0, 250);
|
||||
$time = time();
|
||||
|
||||
if ($title === "") {
|
||||
array_push($_SESSION["error"], "The title was empty.");
|
||||
}
|
||||
if ($name === "") {
|
||||
array_push($_SESSION["error"], "The name was empty.");
|
||||
}
|
||||
if ($comment === "") {
|
||||
array_push($_SESSION["error"], "The comment was empty.");
|
||||
}
|
||||
|
||||
if (count($_SESSION["error"]) > 0) {
|
||||
Headers::redirect(".");
|
||||
return;
|
||||
}
|
||||
|
||||
$string = file_get_contents("../data/data.json") ?? "[]";
|
||||
$json = json_decode($string);
|
||||
|
||||
array_push($json, ["time" => $time, "title" => $title, "name" => $name, "comment" => $comment]);
|
||||
|
||||
file_put_contents("../data/data.json", json_encode($json));
|
||||
|
||||
Headers::redirect("../");
|
||||
3
tasks/guestBookDB/components/.htaccess
Normal file
3
tasks/guestBookDB/components/.htaccess
Normal file
@ -0,0 +1,3 @@
|
||||
Order deny,allow
|
||||
Deny from all
|
||||
Allow from 127.0.0.1
|
||||
15
tasks/guestBookDB/components/comment.php
Normal file
15
tasks/guestBookDB/components/comment.php
Normal file
@ -0,0 +1,15 @@
|
||||
<div class="col">
|
||||
<div class="card h-100 shadow-sm">
|
||||
<div class="card-header">
|
||||
<h6 class="card-subtitle mb-2 text-muted"><?= htmlspecialchars($comment["name"]) ?></h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text"><?= nl2br(htmlspecialchars($comment["comment"])) ?></p>
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
<small class="text-muted">
|
||||
<?= $comment["time"] ?>
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
7
tasks/guestBookDB/components/error.php
Normal file
7
tasks/guestBookDB/components/error.php
Normal file
@ -0,0 +1,7 @@
|
||||
<div class="alert alert-danger alert-dismissible fade show mt-3 mb-0">
|
||||
<svg class="bi flex-shrink-0 me-2" width="24" height="24">
|
||||
<use xlink:href="#exclamation-triangle-fill" />
|
||||
</svg>
|
||||
<?= $error ?>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
30
tasks/guestBookDB/components/newComment.php
Normal file
30
tasks/guestBookDB/components/newComment.php
Normal file
@ -0,0 +1,30 @@
|
||||
<form action="" method="post" class="needs-validation row g-3 m-0 position-relative" novalidate>
|
||||
<h1 class="text-danger position-absolute construction">Under Construction</h1>
|
||||
<fieldset disabled class="mt-0 opacity-50">
|
||||
<div class="col m-0 g-3">
|
||||
<h4 class="mb-3">Leave a Comment!</h4>
|
||||
<div class="mb-3">
|
||||
<input class="form-control" type="text" name="title" required placeholder="A catching title" />
|
||||
<div class="invalid-feedback">
|
||||
Please enter a title.
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<input class="form-control" type="text" name="name" required placeholder="Your name" />
|
||||
<div class="invalid-feedback">
|
||||
Please enter a name.
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-1">
|
||||
<textarea class="form-control" id="comment" name="comment" maxlength="250" required placeholder="Comment here"></textarea>
|
||||
<div class="invalid-feedback">
|
||||
Please enter text.
|
||||
</div>
|
||||
</div>
|
||||
<div class="progress mb-2">
|
||||
<div class="progress-bar" id="comment-count" style="width: 0;"></div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary mb-3">Comment</button>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
19
tasks/guestBookDB/components/pagination.php
Normal file
19
tasks/guestBookDB/components/pagination.php
Normal file
@ -0,0 +1,19 @@
|
||||
<nav>
|
||||
<ul class="pagination justify-content-center">
|
||||
<li class="page-item">
|
||||
<a class="page-link <?php if ($page === 0) echo "disabled" ?>" href="?p=<?= $page > 0 ? $page - 1 : 0 ?>">
|
||||
<span aria-hidden="true">«</span>
|
||||
</a>
|
||||
</li>
|
||||
<?php for ($i = 0; $i <= $maxPage; $i++) { ?>
|
||||
<li class="page-item <?php if ($page === $i) echo "active" ?>">
|
||||
<a class="page-link" href="?p=<?= $i ?>"><?= $i + 1 ?></a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
<li class="page-item">
|
||||
<a class="page-link <?php if ($page === $maxPage) echo "disabled" ?>" href="?p=<?= $page < $maxPage ? $page + 1 : $maxPage ?>">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
18
tasks/guestBookDB/css/styles.css
Normal file
18
tasks/guestBookDB/css/styles.css
Normal file
@ -0,0 +1,18 @@
|
||||
.opacity-50 {
|
||||
opacity: 50%
|
||||
}
|
||||
|
||||
.construction {
|
||||
z-index: 10;
|
||||
rotate: -10deg;
|
||||
width: fit-content;
|
||||
height: fit-content;
|
||||
left: 0;
|
||||
right: 0;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
margin-top: auto;
|
||||
margin-bottom: auto;
|
||||
}
|
||||
85
tasks/guestBookDB/index.php
Normal file
85
tasks/guestBookDB/index.php
Normal file
@ -0,0 +1,85 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<?php include_once "../../base/meta.php" ?>
|
||||
|
||||
<title>Guest Book DB</title>
|
||||
|
||||
<script src="./js/formValidation.js"></script>
|
||||
<script src="./js/textarea.js"></script>
|
||||
|
||||
<link href="./css/styles.css" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<?php
|
||||
// HTML
|
||||
include_once "../../base/icons.php";
|
||||
// PHP
|
||||
include_once "../../base/settings.php";
|
||||
include_once "../../base/headers.php";
|
||||
Headers::html();
|
||||
include_once "../../base/database.php";
|
||||
|
||||
include_once "./queries.php";
|
||||
|
||||
session_name("PHP_SESSION_guestBook");
|
||||
session_start();
|
||||
$errors = $_SESSION["error"] ?? [];
|
||||
$_SESSION["error"] = [];
|
||||
|
||||
if (isset($_GET["p"]) && !is_numeric($_GET["p"])) {
|
||||
Headers::redirect(".");
|
||||
return;
|
||||
}
|
||||
$page = isset($_GET["p"]) ? intval($_GET["p"]) : 0;
|
||||
|
||||
$db = DB::openConnection();
|
||||
|
||||
|
||||
|
||||
$stmt = $db->prepare($countQuery);
|
||||
$stmt->execute();
|
||||
$maxPage = intdiv($stmt->fetch(PDO::FETCH_COLUMN), 9);
|
||||
if ($page > $maxPage) {
|
||||
Headers::redirect("./?p=$maxPage");
|
||||
return;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($dataQuery . $page * 9);
|
||||
$stmt->execute();
|
||||
$data = $stmt->fetchAll();
|
||||
|
||||
DB::closeConnection($db);
|
||||
?>
|
||||
|
||||
<div class="container-fluid p-0">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">Guest Book</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="row m-0">
|
||||
<div class="col">
|
||||
<?php foreach ($errors as $error) {
|
||||
include "./components/error.php";
|
||||
} ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row row-cols-1 row-cols-lg-3 g-3 m-0 mb-3">
|
||||
<?php foreach ($data as $comment) {
|
||||
include "./components/comment.php";
|
||||
} ?>
|
||||
</div>
|
||||
|
||||
<?php include "./components/pagination.php" ?>
|
||||
|
||||
<hr />
|
||||
<?php include "./components/newComment.php" ?>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
22
tasks/guestBookDB/js/formValidation.js
Normal file
22
tasks/guestBookDB/js/formValidation.js
Normal file
@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const handler = (event) => {
|
||||
if (!event.target.checkValidity()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
event.target.classList.add("was-validated");
|
||||
};
|
||||
|
||||
const addValidation = () => {
|
||||
const forms = document.querySelectorAll(".needs-validation");
|
||||
|
||||
Array.from(forms).forEach((form) => {
|
||||
form.addEventListener("submit", handler, false);
|
||||
});
|
||||
};
|
||||
|
||||
addValidation();
|
||||
});
|
||||
35
tasks/guestBookDB/js/textarea.js
Normal file
35
tasks/guestBookDB/js/textarea.js
Normal file
@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const handlerCounter = (event) => {
|
||||
const comment = event.target;
|
||||
const length = comment.value.length;
|
||||
const maxLength = comment.maxLength;
|
||||
const counter = document.getElementById("comment-count");
|
||||
|
||||
if (length >= 50) {
|
||||
counter.innerText = `${length}/${maxLength}`;
|
||||
} else {
|
||||
counter.innerText = "";
|
||||
}
|
||||
counter.style.width = `${(length / maxLength) * 100}%`;
|
||||
|
||||
if (length === maxLength) {
|
||||
counter.classList.remove("bg-warning");
|
||||
counter.classList.add("bg-danger");
|
||||
} else if (length > maxLength - 50) {
|
||||
counter.classList.remove("bg-danger");
|
||||
counter.classList.add("bg-warning");
|
||||
} else {
|
||||
counter.classList.remove("bg-warning");
|
||||
counter.classList.remove("bg-danger");
|
||||
}
|
||||
};
|
||||
|
||||
const addValidation = () => {
|
||||
const comment = document.getElementById("comment");
|
||||
comment.addEventListener("input", handlerCounter);
|
||||
};
|
||||
|
||||
addValidation();
|
||||
});
|
||||
21
tasks/guestBookDB/queries.php
Normal file
21
tasks/guestBookDB/queries.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
$dataQuery = "
|
||||
SELECT
|
||||
B.beitrag AS comment,
|
||||
DATE_FORMAT(B.zeitstempel, '%d.%m.%y %H:%i') AS time,
|
||||
U.benutzer AS name
|
||||
FROM
|
||||
egb_gaestebuch AS B,
|
||||
egb_benutzer AS U
|
||||
WHERE
|
||||
B.benutzer_id = U.id
|
||||
ORDER BY
|
||||
B.zeitstempel DESC
|
||||
LIMIT 9
|
||||
OFFSET ";
|
||||
|
||||
$countQuery = "
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
egb_gaestebuch";
|
||||
Loading…
x
Reference in New Issue
Block a user