forked from 4iz278/cviceni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.php
112 lines (67 loc) · 1.91 KB
/
cart.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
# pripojeni do db
require 'db.php';
# pristup jen pro prihlaseneho uzivatele
require 'user_required.php';
$ids = $_SESSION['cart'];
if (is_array($ids) && count($ids)>0) {
# retezec s otazniky pro predani seznamu ids
# pocet otazniku = pocet prvku v poli ids
# pokud mam treba v ids 1,2,3, vrati mi ?,?,?
$question_marks = str_repeat('?,', count($ids) - 1) . '?';
$stmt = $db->prepare("SELECT * FROM goods WHERE id IN ($question_marks) ORDER BY name");
# array values - setrepeme pole aby bylo indexovane od 0, jen kvuli dotazu, jinak neprojde
$stmt->execute(array_values($ids));
$goods = $stmt->fetchAll();
$stmt_sum = $db->prepare("SELECT SUM(price) FROM goods WHERE id IN ($question_marks)");
# array values - setrepeme pole aby bylo indexovane od 0, jen kvuli dotazu, jinak neprojde
$stmt_sum->execute(array_values($ids));
$sum = $stmt_sum->fetchColumn();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>PHP Shopping App</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php include 'navbar.php' ?>
<h1>My shopping cart</h1>
Total goods selected: <?= count($goods) ?>
<br/><br/>
<a href="index.php">Back to the goods</a>
<br/><br/>
<?php if($goods) { ?>
<table>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
<tfoot>
<tr>
<th>SUM</th>
<th></th>
<th class="right"><?= $sum ?></th>
<th></th>
</tr>
</tfoot>
<?php foreach($goods as $row) { ?>
<tr>
<td class="center">
<a href='remove.php?id=<?= $row['id'] ?>'>Remove</a>
</td>
<td><?= $row['name'] ?></td>
<td class="right"><?= $row['price'] ?></td>
<td><?= $row['description'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
No goods yet
<?php } ?>
</body>
</html>