assignments/Assignment_005/solution/index.php

165 lines
4.2 KiB
PHP

<?php
require_once("Film.php");
session_start();
if (isset($_REQUEST["reset"])) {
unset($_SESSION["filme"]);
unset($_SESSION["sort"]);
}
$lieblingsfilme = $_SESSION["filme"];
if (!isset($lieblingsfilme)) {
$lieblingsfilme = [
new Film("Pulp Fiction", "Quentin Tarantino", 1994, 148, 16),
new Film("Inglourious Basterds", "Quentin Tarantino", 2009, 148, 16),
new Film("Reservoir Dogs", "Quentin Tarantino", 1992, 100, 18),
new Film("Blade Runner", "Ridley Scott", 1982, 117, 16) ];
$_SESSION["filme"] = $lieblingsfilme;
}
if (isset($_REQUEST["delete"])) {
$index = $_REQUEST["delete"];
array_splice($lieblingsfilme, $index, 1);
$_SESSION["filme"] = $lieblingsfilme;
}
elseif (isset($_REQUEST["save"])) {
$film = new Film(
$_REQUEST["titel"],
$_REQUEST["regisseur"],
(int) $_REQUEST["jahr"],
(int) $_REQUEST["spielzeit"],
(int) $_REQUEST["fsk"]);
$lieblingsfilme[] = $film;
$_SESSION["filme"] = $lieblingsfilme;
}
elseif (isset($_REQUEST["sort"])) {
$kriterium = $_REQUEST["sort"];
$_SESSION["sort"] = $kriterium;
}
if (!isset($kriterium)) {
$kriterium = $_SESSION["sort"];
if (!isset($kriterium)) {
$kriterium = "titel";
$_SESSION["sort"] = $kriterium;
}
}
uasort($lieblingsfilme, 'cmp');
$_SESSION["filme"] = $lieblingsfilme;
function cmp($a, $b) {
global $kriterium;
if ($a->equals($b)) {
return 0;
}
if ($kriterium == 'titel') {
return $a->titel > $b->titel ? 1 : -1;
}
elseif ($kriterium == 'regisseur') {
return $a->regisseur > $b->regisseur ? 1 : -1;
}
elseif ($kriterium == 'jahr') {
return $a->produktionsjahr > $b->produktionsjahr ? 1 : -1;
}
elseif ($kriterium == 'spielzeit') {
return $a->laufzeit > $b->laufzeit ? 1 : -1;
}
elseif ($kriterium == 'fsk') {
return $a->altersfreigabe > $b->altersfreigabe ? 1 : -1;
}
}
function showTable($filme) {
print('<table id="filmtable">' . "\n");
print('<tr>' . "\n");
print('<th>#</th>' . "\n");
print('<th><button name="sort" value="titel">Titel</button></th>' . "\n");
print('<th><button name="sort" value="regisseur">Regisseur</button></th>' . "\n");
print('<th><button name="sort" value="jahr">Jahr</button></th>' . "\n");
print('<th><button name="sort" value="spielzeit">Spielzeit</button></th>' . "\n");
print('<th><button name="sort" value="fsk">FSK</button></th>' . "\n");
print('<th>&nbsp;</th>' . "\n");
print('</tr>' . "\n");
$i = 0;
foreach ($filme as $film) {
$count = $i + 1;
print("<tr><td>$count</td>" . "\n");
print("<td>$film->titel</td>" . "\n");
print("<td>$film->regisseur</td>" . "\n");
print("<td>$film->produktionsjahr</td>" . "\n");
print("<td>$film->laufzeit</td>" . "\n");
print("<td>$film->altersfreigabe</td>" . "\n");
print("<td><button name='delete' value='$i'>Löschen</button></td>" . "\n");
print("</tr>");
$i++;
}
print('</table>');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Filmdatenbank</title>
<meta charset="UTF-8">
<style>
table, td, tr, th {
border: 1px solid;
border-color: #d3d3d3;
border-collapse: collapse;
}
</style>
</head>
<body>
<h1>Filmdatenbank</h1>
<form method="POST">
<?php
showTable($lieblingsfilme);
?>
<h2>Eingabe eines neuen Films</h2>
<table>
<tr>
<th>Titel</th>
<td><input type="text" name="titel"></td>
</tr>
<tr>
<th>Regisseur</th>
<td><input type="text" name="regisseur"></td>
</tr>
<tr>
<th>Jahr</th>
<td><input type="text" size="4" name="jahr"></td>
</tr>
<tr>
<th>Spielzeit</th>
<td><input type="text" size="4" name="spielzeit"></td>
</tr>
<tr>
<th>FSK</th>
<td><input type="text" size="2" name="fsk"></td>
</tr>
</table>
<button type="submit" name="save">Speichern</button>
<br>
<br>
<button type="submit" name="reset">Session löschen</button>
</form>
</body>
</html>