Files
Rezepte/index.php
2025-09-20 16:01:52 +02:00

208 lines
6.1 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'db_connection.php';
// Suchbegriff ermitteln
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
// Erlaubte Sortierfelder
$sort_allowed = ['Rezeptnummer', 'Bezeichnung', 'Kategorie'];
// Sortierparameter aus URL oder Standard auf 'Rezeptnummer'
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sort_allowed) ? $_GET['sort'] : 'Rezeptnummer';
// SQL Query aufbauen
$sql = "SELECT id, Rezeptnummer, Bezeichnung, Kategorie FROM Rezepte";
$conditions = [];
$params = [];
$types = '';
// Suche in Bezeichnung, Beschreibung, Kategorie
if (!empty($search)) {
$search_term = "%" . $search . "%";
$conditions[] = "(Bezeichnung LIKE ? OR Beschreibung LIKE ? OR Kategorie LIKE ?)";
$params = array_merge($params, [$search_term, $search_term, $search_term]);
$types .= 'sss';
}
if (!empty($conditions)) {
$sql .= " WHERE " . implode(" AND ", $conditions);
}
// Sortierung: numerisch für Rezeptnummer, sonst alphabetisch
if ($sort === 'Rezeptnummer') {
$sql .= " ORDER BY CAST(`$sort` AS UNSIGNED) ASC";
} else {
$sql .= " ORDER BY `$sort` ASC";
}
$stmt = $link->prepare($sql);
if (!$stmt) {
die("Fehler bei der Vorbereitung der Abfrage: " . $link->error);
}
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html>
<head>
<title>Rezepte Übersicht</title>
<style>
body {
font-family: sans-serif;
background: #fcf6e3;
color: #5b4636;
font-size: 22px;
}
.tabelle-container {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
.table-aufgaben {
width: 900px;
margin: 0 auto;
border-collapse: collapse;
table-layout: fixed;
background: #fff;
}
.table-aufgaben th, .table-aufgaben td {
padding: 8px;
word-wrap: break-word;
overflow-wrap: break-word;
white-space: normal;
text-align: center;
}
.table-aufgaben th:first-child, .table-aufgaben td:first-child {
width: 10%;
text-align: center;
}
.table-aufgaben th.rezeptnr, .table-aufgaben th.bezeichnung {
text-align: center;
}
.table-aufgaben th.bezeichnung {
text-align: left;
}
.table-aufgaben td.bezeichnung {
text-align: left;
}
.table-aufgaben th a {
color: #8b5a2b;
text-decoration: none;
}
.table-aufgaben td.bezeichnung a {
color: #c17e1b;
text-decoration: none;
}
.table-aufgaben td.bezeichnung a:hover {
text-decoration: underline;
}
.table-aufgaben th.kategorie, .table-aufgaben td.kategorie {
width: 30%;
text-align: left;
}
tr {
background: #fbeee6;
}
.search-container {
text-align: center;
margin-bottom: 20px;
}
.search-container form {
display: inline-flex;
align-items: center;
}
.search-container input[type="text"] {
padding: 8px;
font-size: 18px;
width: 15ch;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
}
.search-container button {
padding: 8px 16px;
background: #bb7e19;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 18px;
}
.search-container button:hover {
background: #a06d15;
}
.neu-container {
width: 100%;
display: flex;
justify-content: center;
margin-top: 22px;
}
.neu {
background: #bb7e19;
color: #fff;
padding: 10px 22px;
border: none;
border-radius: 4px;
text-decoration: none;
font-size: 1.5em;
box-shadow: 1px 1px 4px #efd7b1;
}
.edit-button {
display: inline-block;
padding: 10px 20px;
background-color: #2196F3;
color: white;
text-decoration: none;
border-radius: 5px;
font-family: Roboto, sans-serif;
font-size: 1em;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="top-bar">
<!-- <a href="Edit.php" class="neu">Neues Rezept</a>-->
<div class="search-container">
<form method="get" action="">
<input type="text" name="search" value="<?= htmlspecialchars($search) ?>" placeholder="Suche...">
<button type="submit" class="search-btn">Suche</button>
</form>
</div>
</div>
<div class="tabelle-container">
<table class="table-aufgaben">
<tr>
<th class="rezeptnr"><a href="?sort=Rezeptnummer&search=<?= htmlspecialchars($search) ?>">Rezeptnr.</a></th>
<th class="bezeichnung"><a href="?sort=Bezeichnung&search=<?= htmlspecialchars($search) ?>">Bezeichnung</a></th>
<th class="kategorie"><a href="?sort=Kategorie&search=<?= htmlspecialchars($search) ?>">Kategorie</a></th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= htmlspecialchars($row['Rezeptnummer']) ?></td>
<td class="bezeichnung"><a href="main.php?id=<?= htmlspecialchars($row['id']) ?>"><?= htmlspecialchars($row['Bezeichnung']) ?></a></td>
<td class="kategorie"><?= htmlspecialchars($row['Kategorie']) ?></td>
</tr>
<?php endwhile; ?>
</table>
<div class="neu-container">
<a href="Edit.php" class="neu">Neues Rezept</a>
</div>
</div>
</body>
</html>