-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcart.php
More file actions
26 lines (25 loc) · 835 Bytes
/
cart.php
File metadata and controls
26 lines (25 loc) · 835 Bytes
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
<?php
session_start();
//lấy id của hàng cần mua
$id = $_GET['id'];
$name = $_GET['name'];
$price = $_GET['price'];
//Kiểm tra xem đã có giỏ hàng chưa
if(isset($_SESSION['cart']))//đã có giỏ thì lấy ra
{
$cart = $_SESSION['cart'];
}
else{//chưa có thì tạo
$cart = [];
}
//Kiểm tra hàng có trong giỏ chưa
if(array_key_exists($id, $cart)){//hàng đã có trong giỏ
$cart[$id]['quantity']++;
}
else{//chưa có sp trong giỏ
$cart[$id] = array('name'=>$name,'quantity'=>1,'price'=>$price);
}
//cập nhật lại giỏ hàng
$_SESSION['cart'] = $cart;
header("Location: index.php");
?>