Malý repozitář do hodin WTL pro 3.J skupinu WEB
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

77 lines
2.4 KiB

<?php require "header.php";
$search_term = "";
$books = [];
try {
if (isset($_GET["search"]) && !empty(trim($_GET["search"]))){
//logika když hledáme
$search_term = trim($_GET["search"]);
$query = $pdo->prepare("SELECT id, title,
author FROM books
WHERE title LIKE :search OR author LIKE :search
ORDER BY title ASC");
$query->execute(['search' => "%".$search_term."%"]);
$books = $query->fetchAll(PDO::FETCH_ASSOC);
} else {
// logika když NEhledáme
$query = $pdo->prepare("SELECT id, title,
author FROM books ORDER BY title ASC");
$query->execute();
$books = $query->fetchAll(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger"> Chyba při načítaní
dat z db: ' . $e->getMessage() . '<div>';
$books =[];
}
?>
<h4 class="display-3">Seznam Knih</h4>
<!-- <p>Počet knih v databázi: <b><?php echo count($books);?></b></p> -->
<form action="index.php" method="get" class="mb-4">
<div class="input-group">
<input type="text" name="search" class="form-control"
placeholder="Hledej autora nebo knihu..."
value="<?php html_escape($search_term)?>">
<button class="btn btn-primary" type="submit">Hledat</button>
</div>
</form>
<?php if (isset($_GET["status"]) && $_GET["status"] == "added"): ?>
<div class="alert alert-success" role="alert">
Kniha byla úspěšně přídaná! (ID: <?= isset($_GET["id"]) ? html_escape($_GET["id"]) : "N/A" ?>)
</div>
<?php endif; ?>
<?php if (count($books) > 0):?>
<table class="table table-striped table-hover">
<thead>
<th>ID</th>
<th>Název</th>
<th>Autor</th>
</thead>
<tbody>
<?php foreach($books as $book):?>
<tr>
<td><?=html_escape($book["id"])?></td>
<td><?=html_escape($book["title"])?></td>
<td><?=html_escape($book["author"])?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
<?php elseif(empty($e)):?>
<div class="alert alert-info">
<?php if ($search_term):?>
<p>Nebyly nalezeny žádné knihy jako: <?= html_escape($search_term)?></p>
<?php else:?>
<p>V knihovně nejsou knihy.</p>
<?php endif;?>
</div>
<?php endif;?>
<?php require "footer.php";?>