This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
148 lines (129 loc) · 4.32 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
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
<?php
session_start();
$title = "Thông tin mua sách";
require_once("./includes/connect.php");
include("./includes/function.php");
include("./includes/header.php");
include("./includes/sidebar-a.php");
?>
<meta charset="utf-8">
<link rel="stylesheet" href="style/cart.css">
<?php
// Gán biến cho SESSION giỏ hàng
if(isset($_SESSION['giohang']))
{
$giohang = $_SESSION['giohang'];
}
// Xử lý cập nhật
if (isset($_POST['capnhat']) && count($_SESSION['giohang']) != "")
{
$soluong_cn = $_POST['soluong'];
foreach ($soluong_cn as $id => $sl)
{
// Lấy số lượng trong CSDL.
$max = mysql_fetch_assoc(mysql_query("SELECT soluong FROM sach WHERE sach_id = '$id'"));
$max2 = $max['soluong'];
// Nếu như người dùng đặt lại số lượng = 0 Thì ta hủy luôn
if ($sl == 0)
{
// Nếu số lượng = 0 thì xóa hàng đó.
unset($_SESSION['giohang'][$id]);
}
else
{
// Ngược lại số lượng mà > 0 thì ta cập nhật số lượng cho sản phẩm.
$_SESSION['giohang'][$id] = $sl;
}
if (!is_numeric($sl) || $sl < 0) // Nếu số lượng ko phải là số và là số âm thì gán là 1.
{
$_SESSION['giohang'][$id] = 1;
}
elseif ($sl > $max2) // Nếu số lượng vượt quá cho phép thì gán là tối đa.
{
$_SESSION['giohang'][$id] = $max2;
}
// refresh lại trang.
echo "<script>window.location.href='cart.php'</script>";
}// End foreach
}// End $_POST['capnhat']
?>
<div class="noidung2">
<?php
// Kiểm tra số lượng giỏ hàng
if(isset($_SESSION['giohang'])&&count($_SESSION['giohang']) > 0)
{
?>
<table style="width: 77%">
<br />
<h2>Đây là giỏ hàng của bạn</h2>
<form action="" method="post">
<caption>Hiện tại bạn có
<?php
echo "<strong>".count($_SESSION['giohang'])."</strong>";
?>
sản phẩm trong giỏ hàng</caption>
<thead>
<tr class="table_title">
<th>STT</th>
<th>Hình</th>
<th>Tên sản phẩm</th>
<th>Số lượng</th>
<th>Đơn giá (VNĐ)</th>
<th>Thành tiền(VNĐ)</th>
<th>Hủy</th>
</tr>
</thead>
<tbody>
<?php
// Hiển thị giỏ hàng
$tt = 1;
$thanhtien = 0;
foreach ($giohang as $id => $sl)
{
$sql = mysql_query("SELECT * FROM sach WHERE sach_id in ('$id')")
or die("Cannot select table SACH");
$rows = mysql_fetch_assoc($sql);
//---- Tinh so tien ban ---//
$a = $rows['dongia'];
$b = $rows['giamgia'] * 0.01;
$c = $a * $b;
$ban = $a - $c;
echo "<tr class='tr_td'>";
echo "<td>".$tt++."</td>";
echo "<td><img src='./style/images/img_sach/".$rows['hinhanh']."' width='70px;'></td>";
echo "<td>".$rows['sach_name']."</td>";
echo "<td><input type='text' name='soluong[".$id."]' value='".$sl."' style='width:50px;'></td>";
echo "<td>".number_format($ban,"0",",",".")."đ</td>";
echo "<td>".number_format($ban*$sl,"0",",",".")."đ</td>";
echo "<td><a href='xoa_cart.php?sid=".$id."' title='xóa sản phẩm này'><input type='button' value='Xoa' class='xoa_sl'></a></td>";
echo "</tr>";
// Tổng tiền
$thanhtien += $ban*$sl;
}// End foreach
echo '
<tr class="tr_td">
<td colspan="2"><input type="submit" name="capnhat" value="cap nhat" class="update_sl" /></td>
<td colspan="2" style="font-weight:bold;">Tổng số tiền: </td>
<td colspan="3" style="color:red;font-weight:bold;">'.number_format($thanhtien,"0",",",".").'đ</td>
</tr>
';
?>
</tbody>
</form>
</table>
<br />
<center>
<form action="chon_thanhtoan.php" method="POST">
<input type="submit" name="Submit" value="Thực hiện thanh toán" class="button_thanh_toan">
</form>
</center>
<a href="index.php" title="Quay lại trang chủ" class="button_thanh_toan">Tiếp tục mua sách</a>
<?php
}
else
{// Nếu không tồn tại SESSION['giohang'] thì báo ra là:
echo "<span class='error'><br />Bạn không có sản phẩm nào trong giỏ hàng !</span>";
}
?>
</div><!-- End noidung -->
<?php include("./includes/footer.php"); ?>