使用資料庫持久化購物車數據,以 session_id 區分用戶,封裝增、刪、改、查、統計等操作,便於在多頁面復用。
-- 商品表
CREATE TABLE inventory (
id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
product TINYTEXT NOT NULL,
description TINYTEXT NOT NULL,
price FLOAT(10,2) DEFAULT '0.00' NOT NULL,
category CHAR(1) DEFAULT '' NOT NULL,
KEY price (price)
);
-- 購物車表
CREATE TABLE shopping (
id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
session TINYTEXT NOT NULL,
product TINYTEXT NOT NULL,
quantity TINYTEXT NOT NULL,
KEY id (id)
);
<?php
class Cart {
// 檢查商品是否已在購物車
function check_item($table, $session, $product) {
$query = "SELECT * FROM $table WHERE session='$session' AND product='$product'";
$result = mysql_query($query);
if (!$result) return 0;
$num = mysql_num_rows($result);
if ($num == 0) return 0;
$row = mysql_fetch_object($result);
return $row->quantity;
}
// 添加/更新商品數量
function add_item($table, $session, $product, $quantity) {
$qty = $this->check_item($table, $session, $product);
if ($qty == 0) {
$query = "INSERT INTO $table (session, product, quantity) VALUES ('$session', '$product', '$quantity')";
} else {
$quantity += $qty;
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND product='$product'";
}
mysql_query($query);
}
// 刪除商品
function delete_item($table, $session, $product) {
$query = "DELETE FROM $table WHERE session='$session' AND product='$product'";
mysql_query($query);
}
// 修改數量
function modify_quantity($table, $session, $product, $quantity) {
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND product='$product'";
mysql_query($query);
}
// 清空購物車
function clear_cart($table, $session) {
$query = "DELETE FROM $table WHERE session='$session'";
mysql_query($query);
}
// 計算總價
function cart_total($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session'";
$result = mysql_query($query);
$total = 0;
while ($row = mysql_fetch_object($result)) {
$q = "SELECT price FROM inventory WHERE product='$row->product'";
$r = mysql_query($q);
$p = mysql_fetch_object($r);
$total += $p->price * $row->quantity;
}
return $total;
}
// 獲取購物車詳情(含商品信息)
function display_contents($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id";
$result = mysql_query($query);
$contents = array(); $count = 0;
while ($row = mysql_fetch_object($result)) {
$q = "SELECT * FROM inventory WHERE product='$row->product'";
$r = mysql_query($q);
$inv = mysql_fetch_object($r);
$contents['product'][$count] = $inv->product;
$contents['price'][$count] = $inv->price;
$contents['quantity'][$count] = $row->quantity;
$contents['total'][$count] = $inv->price * $row->quantity;
$contents['description'][$count] = $inv->description;
$count++;
}
$contents['final'] = $this->cart_total($table, $session);
return $contents;
}
// 獲取商品種類數
function num_items($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session'";
$result = mysql_query($query);
return mysql_num_rows($result);
}
// 獲取商品總件數
function quant_items($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session'";
$result = mysql_query($query);
$quant = 0;
while ($row = mysql_fetch_object($result)) $quant += $row->quantity;
return $quant;
}
}
?>
<?php
session_start();
if (!isset($_SESSION['scid'])) {
$_SESSION['scid'] = md5(uniqid(rand()));
setcookie('scid', $_SESSION['scid'], time()+14400); // 4 小時
}
include 'shoppingcart.php';
$cart = new Cart();
$link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('shop_db', $link);
// 添加商品
$cart->add_item('shopping', $_SESSION['scid'], 'P001', 2);
// 顯示購物車
$items = $cart->display_contents('shopping', $_SESSION['scid']);
foreach ($items['product'] as $i => $prod) {
echo "$prod x {$items['quantity'][$i]} = {$items['total'][$i]}
";
}
echo "總計: {$items['final']}";
?>
mysqli/PDO 預處理語句。mysql_* 函數已棄用(PHP 7+ 移除),請遷移至 mysqli 或 PDO。session_id(),無需自行生成 Cookie。