-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
71 lines (66 loc) · 2.04 KB
/
index.php
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
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<?php
include "./header.shtml";
require_once "db.php";
?>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="content">
<?php
$res = getCards();
foreach($res as $card): ?>
<a href="./item.php?id=<?php echo $card["id"] ?>">
<div class="card">
<div class="card-photo">
<img src="<?php echo $card["img"] ?>" alt="">
</div>
<div class="card-title">
<?php echo $card["title"] ?>
</div>
<div class="card-price">
<?php echo getPriceRange(getVariants($card["id"])) . "$"?>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
</body>
</html>
<?php
function getCards() { //TODO: Sort/search functionality
global $conn;
$result = $conn->query("SELECT * FROM `products` WHERE 1");
return $result;
}
function getVariants($id) {
global $conn;
$stmt = $conn->prepare("SELECT * FROM `variants` WHERE `parent`=?");
$stmt->bind_param("i", $id);
$stmt->execute();
return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
function getProductData($variants) {
}
function getPriceRange($variants) {
$minPrice = $variants[0]["price"];
$maxPrice = $variants[0]["price"];
foreach($variants as $curr) {
if($curr["price"] > $maxPrice) {
$maxPrice = $curr["price"];
} else if($curr["price"] < $minPrice) {
$minPrice = $curr["price"];
}
}
$priceStr = "";
if($minPrice === $maxPrice) {
$priceStr = $maxPrice;
} else {
$priceStr = $minPrice . " - " . $maxPrice;
}
return $priceStr;
}
?>