首頁 後端開發 php教程 PHP MySQL 购物车程序实例_PHP

PHP MySQL 购物车程序实例_PHP

Jun 01, 2016 pm 12:31 PM
query session table 實例 程式 購物車


if(!$session && !$scid) {
$session = md5(uniqid(rand()));
SetCookie("scid", "$session", time() 14400);
} /* last number is expiration time in seconds, 14400 sec = 4 hrs */

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;
}

$numRows = mysql_num_rows($result);

if($numRows == 0) {
return 0;
} else {
$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 ";
$query .= "('$session', '$product', '$quantity') ";
mysql_query($query);
} else {
$quantity = $qty;
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND ";
$query .= "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' ";
$query .= "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);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)) {
$query = "SELECT price FROM inventory WHERE product='$row->product' ";
$invResult = mysql_query($query);
$row_price = mysql_fetch_object($invResult);
$total = ($row_price->price * $row->quantity);
}
}
return $total;
}

function display_contents($table, $session) {
$count = 0;
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$query = "SELECT * FROM inventory WHERE product='$row->product' ";
$result_inv = mysql_query($query);
$row_inventory = mysql_fetch_object($result_inv);
$contents["product"][$count] = $row_inventory->product;
$contents["price"][$count] = $row_inventory->price;
$contents["quantity"][$count] = $row->quantity;
$contents["total"][$count] = ($row_inventory->price * $row->quantity);
$contents["description"][$count] = $row_inventory->description;
$count ;
}
$total = $this->cart_total($table, $session);
$contents["final"] = $total;
return $contents;
}

function num_items($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
return $num_rows;
}

function quant_items($table, $session) {
$quant = 0;
$query = "SELECT * FROM $table WHERE session='$session' ";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$quant = $row->quantity;
}
return $quant;
}
}
?>

/*
This part contains a description of how to create the tables on your mysql server.

# MySQL dump 6.0
#
# Host: localhost Database: kmartShopper
#--------------------------------------------------------
# Server version 3.22.25

#
# Table structure for table 'inventory'
#
CREATE TABLE inventory (
product tinytext NOT NULL,
quantity tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
description tinytext NOT NULL,
price float(10,2) DEFAULT '0.00' NOT NULL,
category char(1) DEFAULT '' NOT NULL,
KEY id (id),
PRIMARY KEY (id),
KEY price (price)
);

#
# Table structure for table 'shopping'
#
CREATE TABLE shopping (
session tinytext NOT NULL,
product tinytext NOT NULL,
quantity tinytext NOT NULL,
card tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
KEY id (id),
PRIMARY KEY (id)
);
*/

Example

include("shoppingcart.php");
$cart = new Cart;
$mysql_link = mysql_connect("localhost", "wwwrun", "");
$mysql_select_db("kmartShopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartShopper */
?>
/* call functions like $cart->add_item and such, see the code. */
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何在iPhone中使Google地圖成為預設地圖 如何在iPhone中使Google地圖成為預設地圖 Apr 17, 2024 pm 07:34 PM

iPhone上的預設地圖是Apple專有的地理位置供應商「地圖」。儘管地圖越來越好,但它在美國以外的地區運作不佳。與谷歌地圖相比,它沒有什麼可提供的。在本文中,我們討論了使用Google地圖成為iPhone上的預設地圖的可行性步驟。如何在iPhone中使Google地圖成為預設地圖將Google地圖設定為手機上的預設地圖應用程式比您想像的要容易。請依照以下步驟操作–先決條件步驟–您必須在手機上安裝Gmail。步驟1–開啟AppStore。步驟2–搜尋“Gmail”。步驟3–點選Gmail應用程式旁

如何透過C++編寫一個簡單的倒數計時程式? 如何透過C++編寫一個簡單的倒數計時程式? Nov 03, 2023 pm 01:39 PM

C++是一種廣泛使用的程式語言,在編寫倒數計時器方面非常方便且實用。倒數計時程式是一種常見的應用,它能為我們提供非常精確的時間計算和倒數功能。本文將介紹如何使用C++來寫一個簡單的倒數計時程式。實現倒數程序的關鍵就是使用計時器來計算時間的流逝。在C++中,我們可以使用time.h頭檔中的函數來實作計時器的功能。下面是一個簡單的倒數計時程式的程式碼

iPhone中缺少時鐘應用程式:如何修復 iPhone中缺少時鐘應用程式:如何修復 May 03, 2024 pm 09:19 PM

您的手機中缺少時鐘應用程式嗎?日期和時間仍將顯示在iPhone的狀態列上。但是,如果沒有時鐘應用程序,您將無法使用世界時鐘、碼錶、鬧鐘等多項功能。因此,修復時鐘應用程式的缺失應該是您的待辦事項清單的首位。這些解決方案可以幫助您解決此問題。修復1–放置時鐘應用程式如果您錯誤地從主畫面中刪除了時鐘應用程序,您可以將時鐘應用程式放回原位。步驟1–解鎖iPhone並開始向左側滑動,直到到達「應用程式庫」頁面。步驟2–接下來,在搜尋框中搜尋「時鐘」。步驟3–當您在搜尋結果中看到下方的「時鐘」時,請按住它並

session失效怎麼解決 session失效怎麼解決 Oct 18, 2023 pm 05:19 PM

session失效通常是由於 session 的生存時間過期或伺服器關閉導致的。其解決方法:1、延長session的生存時間;2、使用持久化儲存;3、使用cookie;4、非同步更新session;5、使用會話管理中介軟體。

無法允許存取 iPhone 中的相機和麥克風 無法允許存取 iPhone 中的相機和麥克風 Apr 23, 2024 am 11:13 AM

您在嘗試使用應用程式時是否收到“無法允許存取攝影機和麥克風”?通常,您可以在需要提供的基礎上向特定物件授予攝影機和麥克風權限。但是,如果您拒絕權限,攝影機和麥克風將無法運作,而是顯示此錯誤訊息。解決這個問題是非常基本的,你可以在一兩分鐘內完成。修復1–提供相機、麥克風權限您可以直接在設定中提供必要的攝影機和麥克風權限。步驟1–轉到“設定”選項卡。步驟2–打開「隱私與安全」面板。步驟3–在那裡打開“相機”權限。步驟4–在裡面,您將找到已要求手機相機權限的應用程式清單。步驟5–開啟指定應用的“相機”

Java中如何實作一個簡單的購物車功能? Java中如何實作一個簡單的購物車功能? Nov 02, 2023 am 11:56 AM

Java中如何實作一個簡單的購物車功能?購物車是線上商店的重要功能,它允許用戶將想要購買的商品添加到購物車中,並對商品進行管理。在Java中,我們可以透過使用物件導向的方式來實作一個簡單的購物車功能。首先,我們需要定義一個商品類。此類別包含商品的名稱、價格和數量等屬性,以及對應的Getter和Setter方法。例如:publicclassProduct

PHP Session 跨域問題的解決方法 PHP Session 跨域問題的解決方法 Oct 12, 2023 pm 03:00 PM

PHPSession跨域問題的解決方法在前後端分離的開發中,跨域請求已成為常態。在處理跨域問題時,我們通常會涉及session的使用和管理。然而,由於瀏覽器的同源策略限制,跨域情況下預設無法共享session。為了解決這個問題,我們需要採用一些技巧和方法來實現session的跨域共享。一、使用cookie跨域共享session最常

實戰教學:PHP與MySQL實作購物車功能詳解 實戰教學:PHP與MySQL實作購物車功能詳解 Mar 15, 2024 pm 12:27 PM

實戰教學:PHP和MySQL實現購物車功能詳解購物車功能是網站開發中常見的功能之一,透過購物車使用者可以輕鬆地將想要購買的商品加入購物車,然後進行結算和付款。在這篇文章中,我們將詳細介紹如何使用PHP和MySQL實作一個簡單的購物車功能,並提供具體的程式碼範例。建立資料庫和資料表首先需要在MySQL資料庫中建立一個用來儲存商品資訊的資料表。以下是一個簡單的數據表

See all articles