-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuy.php
155 lines (117 loc) · 4.63 KB
/
buy.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
session_start();
require_once('./import/database.php');
$stmt = $db->prepare('select * from badgeInfo');
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
for ($i = 0; $i < count($result); $i++) {
$badge_id = $result[$i]['id'];
if (!isset($_POST[$badge_id]))
continue;
$updateSucess = false;
$insertSucess = false;
$amount = (int)$_POST[$badge_id];
if($amount==0)
continue;
$getOrginalResult = $db->prepare('SELECT amount from inventory where user_id = ? and badge_id=?');
$getOrginalResult->execute([$_SESSION['userid'], $badge_id]);
$orginalAmount = $getOrginalResult->fetch();
if (is_array($orginalAmount)) {
$updateStmt = $db->prepare('UPDATE inventory SET amount=? where user_id=? and badge_id = ?');
$updateSucess = $updateStmt->execute([$amount + (int)$orginalAmount['amount'], $_SESSION['userid'], $badge_id]);
} else {//not exist
echo 'error1';
$insertStmt = $db->prepare('INSERT into inventory(amount,user_id,badge_id) values (?,?,?)');
$insertSucess = $insertStmt->execute([$amount, $_SESSION['userid'], $badge_id]);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
include('./import/basic.php');
include('./import/adblock.php');
include('./import/good.php');
?>
</head>
<script>
let total = 0;
$(function () {
$("#buy").on("click", function () {
$('#submit').trigger('click');
})
$("#clear").on("click", function () {
$("input[type=text]").each(function () {
$(this).val("0")
})
})
$(".add").on("click", function () {
let temp = $(this).siblings('input')
temp.val(Number(temp.val()) + 1);
total += Number(temp.attr('price'))
$('#price').text("總共" + total + "元");
})
$(".sub").on("click", function () {
let temp = $(this).siblings('input')
total -= (temp.val() === "0") ? 0 : Number(temp.attr('price'));
temp.val((Number(temp.val()) > 0) ? Number(temp.val()) - 1 : 0);
$('#price').text("總共" + total + "元");
})
})
</script>
<body class="bg-dark" data-bs-theme="dark">
<?php
$page_func = "購買badge";
include('./import/nav.php');
?>
<div class="container-xxl">
<br>
<h1 class="text-center fw-bold"> 購買方案</h1>
<p class="text-center">任何獲得獎章的人將會獲得經驗值獎勵</p>
<hr>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if($updateSucess || $insertSucess)
echo '<h1 class="alert alert-success container profile-edit text-center" role="alert">成功消費</h1>';
}
?>
<form method="post" action="./buy.php">
<div class="text-center row row-cols-1 row-cols-md-2 row-cols-lg-4 vip-info">
<?php
for ($i = 0; $i < count($result); $i++) { ?>
<div class=" p-2 col">
<div class="container rounded shadow purchase-card ">
<img src="./img/good/<?php echo $result[$i]['id'] ?>.png" alt="">
<h1><?php echo $result[$i]['name'] ?></h1>
<p><?php echo $result[$i]['description'] ?></p>
<p><?php echo $result[$i]['price'] ?></p>
<div class="counter center-text">
<div class="input-group">
<button class="btn btn-danger sub" type="button">-</button>
<input type="text" class="form-control" value="0"
name="<?php echo $result[$i]['id'] ?>" price="<?php echo $result[$i]['price'] ?>"
readonly>
<button class="btn btn-danger add" type="button">+</button>
</div>
</div>
</div>
</div>
<?php }
unset($db);
?>
</div>
<hr>
<div class="d-flex justify-content-end align-items-center">
<h2 class="align-self-center me-auto" id="price">總共0元</h2>
<h2 class=" align-self-center btn btn-danger me-2" type="button" id="clear">清除</h2>
<h2 class=" align-self-center btn btn-primary" type="button" id="buy">結帳</h2>
<button class="d-none" type="submit" id="submit"></button>
</div>
</form>
</div>
<br>
</body>
</html>