Article List

This commit is contained in:
Kilian Hofmann 2024-06-07 13:33:01 +02:00
parent f6626019ae
commit 15d3507b04
10 changed files with 236 additions and 59 deletions

View File

@ -1,58 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once "../../base/meta.php" ?>
<title>Artikel</title>
</head>
<body>
<?php
include_once "../../base/settings.php";
include_once "../../base/headers.php";
Headers::html();
include_once "../../base/database.php";
$db = DB::openConnection();
$query = "
SELECT
ArtikelNr, Artikelname, Lagerbestand
FROM
Artikel
ORDER BY
ArtikelNr";
$stmt = $db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
DB::closeConnection($db);
?>
<div class="col">
<table class="table table-sm table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Artikelname</th>
<th scope="col">Lagerbestand</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $item) {
?>
<tr>
<th scope="row"><?= $item["ArtikelNr"] ?></th>
<td><?= $item["Artikelname"] ?></td>
<td><?= $item["Lagerbestand"] ?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>

View File

@ -1 +1 @@
3
4

View File

@ -19,6 +19,7 @@
include_once "../../base/headers.php";
Headers::html();
session_name("PHP_SESSION_guestBook");
session_start();
$errors = $_SESSION["error"] ?? [];
$_SESSION["error"] = [];

View 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">&laquo;</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">&raquo;</span>
</a>
</li>
</ul>
</nav>

View File

@ -0,0 +1,13 @@
<tbody>
<?php
foreach ($result as $item) {
?>
<tr>
<th scope="row"><?= $item["ArtikelNr"] ?></th>
<td><?= $item["Artikelname"] ?></td>
<td><?= number_format($item["Einzelpreis"], 2, ",", ".") ?></td>
</tr>
<?php
}
?>
</tbody>

View File

@ -0,0 +1,15 @@
<thead class="sticky-top table-dark">
<tr>
<form method="post">
<th>
<button class="p-0 text-white fw-bold text-nowrap btn btn-link arrow-<?= $number ?>" name="number" value="<?= $number == "up" ? "down" : "up" ?>" title="<?= $number == "up" ? "Abwärts sortieren" : "Aufwärts sortieren" ?>">#</button>
</th>
<th>
<button class="p-0 text-white fw-bold text-nowrap btn btn-link arrow-<?= $article ?>" name="article" value="<?= $article == "up" ? "down" : "up" ?>" title="<?= $article == "up" ? "Abwärts sortieren" : "Aufwärts sortieren" ?>">Name</button>
</th>
<th>
<button class="p-0 text-white fw-bold text-nowrap btn btn-link arrow-<?= $price ?>" name="price" value="<?= $price == "up" ? "down" : "up" ?>" title="<?= $price == "up" ? "Abwärts sortieren" : "Aufwärts sortieren" ?>">Price per Unit</button>
</th>
</form>
</tr>
</thead>

View File

@ -0,0 +1,34 @@
.arrow-down::after {
display: inline-block;
margin-left: .255em;
vertical-align: .255em;
content: "";
border-top: .3em solid;
border-right: .3em solid transparent;
border-bottom: 0;
border-left: .3em solid transparent;
}
.arrow-up::after {
display: inline-block;
margin-left: .255em;
vertical-align: .255em;
content: "";
border-top: .3em solid;
border-right: .3em solid transparent;
border-bottom: 0;
border-left: .3em solid transparent;
transform: rotate(180deg);
}
.arrow-none::after {
display: inline-block;
margin-left: .255em;
vertical-align: .255em;
content: "";
border-top: .3em solid;
border-right: .3em solid transparent;
border-bottom: 0;
border-left: .3em solid transparent;
color: transparent;
}

126
tasks/itemList/index.php Normal file
View File

@ -0,0 +1,126 @@
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once "../../base/meta.php" ?>
<link href="./css/head.css" rel="stylesheet" />
<title>Artikel</title>
</head>
<body>
<?php
include_once "../../base/settings.php";
include_once "../../base/headers.php";
Headers::html();
include_once "../../base/database.php";
include_once "./queries.php";
$query = $base;
session_name("PHP_SESSION_itemList");
session_start();
$db = DB::openConnection();
// DEFAULTS
$number = "none";
$article = "up";
$price = "none";
$page = 0;
// Set from Session first
if (isset($_SESSION["number"])) {
$number = $_SESSION["number"];
}
if (isset($_SESSION["article"])) {
$article = $_SESSION["article"];
}
if (isset($_SESSION["price"])) {
$price = $_SESSION["price"];
}
// Populate with actual values if set and save to session
$shouldRedirect = false;
if (isset($_POST["number"])) {
$number = $_POST["number"];
$article = "none";
$price = "none";
$shouldRedirect = true;
}
if (isset($_POST["article"])) {
$number = "none";
$article = $_POST["article"];
$price = "none";
$shouldRedirect = true;
}
if (isset($_POST["price"])) {
$number = "none";
$article = "none";
$price = $_POST["price"];
$shouldRedirect = true;
}
// Save to session
$_SESSION["number"] = $number;
$_SESSION["article"] = $article;
$_SESSION["price"] = $price;
if ($shouldRedirect) {
Headers::redirect("./");
return;
}
if ($number !== "none")
$query .= "\nORDER BY {$sorters["number"][$number]}\n";
if ($article !== "none")
$query .= "\nORDER BY {$sorters["article"][$article]}\n";
if ($price !== "none")
$query .= "\nORDER BY {$sorters["price"][$price]}\n";
$query .= "LIMIT 10\n";
$stmt = $db->prepare($cnt);
$stmt->execute();
$maxPage = intdiv($stmt->fetch(PDO::FETCH_COLUMN), 10);
$page = isset($_GET["p"]) ? intval($_GET["p"]) : 0;
if ($page > $maxPage) {
Headers::redirect("./?p=$maxPage");
return;
}
if ($page < 0) {
Headers::redirect("./");
return;
}
$query .= "OFFSET " . $page * 10;
$stmt = $db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
DB::closeConnection($db);
?>
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
<div class="container-fluid">
<a class="navbar-brand" href="#">List of items</a>
</div>
</nav>
<div class="container-fluid">
<p>
Below you will find a list of all purchasable items, their item number as well as their price per unit.
</p>
<main>
<table class="table table-sm table-striped">
<?php include "./components/table/head.php" ?>
<?php include "./components/table/body.php" ?>
</table>
<?php include "./components/pagination.php" ?>
</main>
</div>
</body>
</html>

View File

@ -0,0 +1,27 @@
<?php
$base = "
SELECT
ArtikelNr, Artikelname, Einzelpreis
FROM
Artikel";
$cnt = "
SELECT
COUNT(*)
FROM
Artikel";
$sorters = [
"number" => [
"up" => "ArtikelNr ASC",
"down" => "ArtikelNr DESC",
],
"article" => [
"up" => "Artikelname ASC",
"down" => "Artikelname DESC",
],
"price" => [
"up" => "Einzelpreis ASC",
"down" => "Einzelpreis DESC",
],
];