-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.php
More file actions
60 lines (48 loc) · 1.02 KB
/
catalog.php
File metadata and controls
60 lines (48 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
require_once './php/DataBase.php';
require_once './php/Config.php';
$db = new DataBase();
$dbConnection = $db -> getConnection();
$query = "SELECT * FROM book";
$books = getBooks($query, $dbConnection);
unset($db);
unset($dbConnection);
foreach($books as $book){
showBookCard($book);
}
function getBooks($query, $connection){
$books;
$statement = $connection -> prepare($query);
$statement -> execute();
$books = $statement -> fetchAll(PDO::FETCH_ASSOC);
return $books;
}
function showBookCard($book){
$price = (float) $book['price'];
$stock = (int) $book['stock'];
if(!$price){
$price = "Free :)";
} else {
$price = '$' . $price;
}
if(!$stock){
$stock = "Available soon";
} else {
$stock .= " left.";
}
$card = "
<div class='book-card'>
<img src='{$book['img_uri']}'>
<div class='title'>
<h2>{$book['title']}</h2>
<p>{$book['author']}</p>
</div>
<div class='body'>
<p>{$price}</p>
<p>{$stock}</p>
</div>
</div>
";
echo $card;
}
?>