php 購(gòu)物車(chē)實(shí)例(申精)
更新時(shí)間:2009年05月11日 17:09:57 作者:
網(wǎng)上搜到的,簡(jiǎn)單容易理解。cookie存購(gòu)物車(chē)ID,db存購(gòu)物車(chē)數(shù)據(jù)。 購(gòu)物車(chē)session的產(chǎn)生代碼
if(! $session && ! $scid) {
/*
session用來(lái)區(qū)別每一個(gè)購(gòu)物車(chē),相當(dāng)于每個(gè)車(chē)的身份證號(hào);
scid只用來(lái)標(biāo)識(shí)一個(gè)購(gòu)物車(chē)id號(hào),可以看做是每個(gè)車(chē)的名字;
當(dāng)該購(gòu)物車(chē)的id和session值兩者都不存在時(shí),就產(chǎn)生一個(gè)新購(gòu)物車(chē)
*/
$session = md5(uniqid(rand()));
/*
產(chǎn)生一個(gè)唯一的購(gòu)物車(chē)session號(hào)
rand()先產(chǎn)生個(gè)隨機(jī)數(shù),uniqid()再在該隨機(jī)數(shù)的基礎(chǔ)上產(chǎn)生一個(gè)獨(dú)一無(wú)二的字符串,最后對(duì)該字符串進(jìn)行md5
*/
SetCookie(scid, $session, time() + 14400);
/*
設(shè)置該購(gòu)物車(chē)cookie
變量名:scid(不知到這里是不是少了一個(gè) $號(hào)呢?=》更正:scid要加“”)
變量值: $session
有效時(shí)間:當(dāng)前時(shí)間+14400秒(4小時(shí)內(nèi))
關(guān)于setcookie函數(shù)的詳細(xì)用法,大家還是參看php手冊(cè)吧~
*/
}
class Cart { //開(kāi)始購(gòu)物車(chē)類(lèi)
function check_item( $table, $session, $product) {
/*
查驗(yàn)物品(表名,session,物品)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
看一看'表'里該'購(gòu)物車(chē)'中有沒(méi)有該'產(chǎn)品'
即,該產(chǎn)品有沒(méi)有已經(jīng)放入購(gòu)物車(chē)
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
查詢(xún)失敗
*/
$numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
若沒(méi)有找到,則返回0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
若找到,則返回該物品數(shù)量
這里有必要解釋一下mysql_fetch_object函數(shù)(下面還會(huì)用到):
【mysql_fetch_object() 和 mysql_fetch_array() 類(lèi)似,只有一點(diǎn)區(qū)別 - 返回一個(gè)對(duì)象而不是數(shù)組?!?
上面這句話(huà)摘自php手冊(cè),說(shuō)得應(yīng)該很明白了吧~
簡(jiǎn)單的說(shuō)就是,取一條記錄中的某個(gè)字段,應(yīng)該用“->”而不是像數(shù)組一樣用下標(biāo)
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
添加新物品(表名,session,物品,數(shù)量)
*/
$qty = $this->check_item( $table, $session, $product);
/*
調(diào)用上面那個(gè)函數(shù),先檢查該類(lèi)物品有沒(méi)有已經(jīng)放入車(chē)中
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*若車(chē)中沒(méi)有,則像車(chē)中添加該物品*/
} else {
$quantity += $qty; //若有,則在原有基礎(chǔ)上增加數(shù)量
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
并修改數(shù)據(jù)庫(kù)
*/
}
}
function delete_item( $table, $session, $product) {
/*
刪除物品(表名,session,物品)
*/
$query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
mysql_query( $query);
/*
刪除該購(gòu)物車(chē)中該類(lèi)物品
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
修改物品數(shù)量(表名,session,物品,數(shù)量)
*/
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
將該物品數(shù)量修改為參數(shù)中的值
*/
}
function clear_cart( $table, $session) {
/*
清空購(gòu)物車(chē)(沒(méi)什么好說(shuō))
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
車(chē)中物品總價(jià)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
/*
先把車(chē)中所有物品取出
*/
if(mysql_num_rows( $result) > 0) {
while( $row = mysql_fetch_object( $result)) {
/*
如果物品數(shù)量>0個(gè),則逐個(gè)判斷價(jià)格并計(jì)算
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
從inventory(庫(kù)存)表中查找該物品的價(jià)格
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
總價(jià) += 該物品價(jià)格 * 該物品數(shù)量
( 大家應(yīng)該能看明白吧 )
*/
}
}
return $total; //返回總價(jià)錢(qián)
}
function display_contents( $table, $session) {
/*
獲取關(guān)于車(chē)中所有物品的詳細(xì)信息
*/
$count = 0;
/*
物品數(shù)量計(jì)數(shù)
注意,該變量不僅僅為了對(duì)物品數(shù)量進(jìn)行統(tǒng)計(jì),更重要的是,它將作為返回值數(shù)組中的下標(biāo),用來(lái)區(qū)別每一個(gè)物品!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
先取出車(chē)中所有物品
*/
while( $row = mysql_fetch_object( $result)) {
/*
分別對(duì)每一個(gè)物品進(jìn)行取詳細(xì)信息
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
從inventory(庫(kù)存)表中查找該物品的相關(guān)信息
*/
$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;
/*
把所有關(guān)于該物品的詳細(xì)信息放入 $contents數(shù)組
$contents是一個(gè)二維數(shù)組
第一組下標(biāo)是區(qū)別每個(gè)物品各個(gè)不同的信息(如物品名,價(jià)錢(qián),數(shù)量等等)
第二組下標(biāo)是區(qū)別不同的物品(這就是前面定義的 $count變量的作用)
*/
$count++; //物品數(shù)量加一(即下一個(gè)物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同時(shí)調(diào)用上面那個(gè)cart_total函數(shù),計(jì)算下總價(jià)錢(qián)
并放入 $contents數(shù)組中
*/
return $contents;
/*
將該數(shù)組返回
*/
}
function num_items( $table, $session) {
/*
返回物品種類(lèi)總數(shù)(也就是說(shuō),兩個(gè)相同的東西算一種 好像是廢話(huà)- -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
取出車(chē)中所有物品,獲取該操作影響的數(shù)據(jù)庫(kù)行數(shù),即物品總數(shù)(沒(méi)什么好說(shuō)的)
*/
}
function quant_items( $table, $session) {
/*
返回所有物品總數(shù)(也就是說(shuō),兩個(gè)相同的東西也算兩個(gè)物品 - -#)
*/
$quant = 0;// 物品總量
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
把每種物品逐個(gè)取出
*/
$quant += $row->quantity; //該物品數(shù)量加到總量里去
}
return $quant; //返回總量
}
}
/*
session用來(lái)區(qū)別每一個(gè)購(gòu)物車(chē),相當(dāng)于每個(gè)車(chē)的身份證號(hào);
scid只用來(lái)標(biāo)識(shí)一個(gè)購(gòu)物車(chē)id號(hào),可以看做是每個(gè)車(chē)的名字;
當(dāng)該購(gòu)物車(chē)的id和session值兩者都不存在時(shí),就產(chǎn)生一個(gè)新購(gòu)物車(chē)
*/
$session = md5(uniqid(rand()));
/*
產(chǎn)生一個(gè)唯一的購(gòu)物車(chē)session號(hào)
rand()先產(chǎn)生個(gè)隨機(jī)數(shù),uniqid()再在該隨機(jī)數(shù)的基礎(chǔ)上產(chǎn)生一個(gè)獨(dú)一無(wú)二的字符串,最后對(duì)該字符串進(jìn)行md5
*/
SetCookie(scid, $session, time() + 14400);
/*
設(shè)置該購(gòu)物車(chē)cookie
變量名:scid(不知到這里是不是少了一個(gè) $號(hào)呢?=》更正:scid要加“”)
變量值: $session
有效時(shí)間:當(dāng)前時(shí)間+14400秒(4小時(shí)內(nèi))
關(guān)于setcookie函數(shù)的詳細(xì)用法,大家還是參看php手冊(cè)吧~
*/
}
class Cart { //開(kāi)始購(gòu)物車(chē)類(lèi)
function check_item( $table, $session, $product) {
/*
查驗(yàn)物品(表名,session,物品)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
看一看'表'里該'購(gòu)物車(chē)'中有沒(méi)有該'產(chǎn)品'
即,該產(chǎn)品有沒(méi)有已經(jīng)放入購(gòu)物車(chē)
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
查詢(xún)失敗
*/
$numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
若沒(méi)有找到,則返回0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
若找到,則返回該物品數(shù)量
這里有必要解釋一下mysql_fetch_object函數(shù)(下面還會(huì)用到):
【mysql_fetch_object() 和 mysql_fetch_array() 類(lèi)似,只有一點(diǎn)區(qū)別 - 返回一個(gè)對(duì)象而不是數(shù)組?!?
上面這句話(huà)摘自php手冊(cè),說(shuō)得應(yīng)該很明白了吧~
簡(jiǎn)單的說(shuō)就是,取一條記錄中的某個(gè)字段,應(yīng)該用“->”而不是像數(shù)組一樣用下標(biāo)
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
添加新物品(表名,session,物品,數(shù)量)
*/
$qty = $this->check_item( $table, $session, $product);
/*
調(diào)用上面那個(gè)函數(shù),先檢查該類(lèi)物品有沒(méi)有已經(jīng)放入車(chē)中
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*若車(chē)中沒(méi)有,則像車(chē)中添加該物品*/
} else {
$quantity += $qty; //若有,則在原有基礎(chǔ)上增加數(shù)量
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
并修改數(shù)據(jù)庫(kù)
*/
}
}
function delete_item( $table, $session, $product) {
/*
刪除物品(表名,session,物品)
*/
$query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
mysql_query( $query);
/*
刪除該購(gòu)物車(chē)中該類(lèi)物品
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
修改物品數(shù)量(表名,session,物品,數(shù)量)
*/
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
將該物品數(shù)量修改為參數(shù)中的值
*/
}
function clear_cart( $table, $session) {
/*
清空購(gòu)物車(chē)(沒(méi)什么好說(shuō))
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
車(chē)中物品總價(jià)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
/*
先把車(chē)中所有物品取出
*/
if(mysql_num_rows( $result) > 0) {
while( $row = mysql_fetch_object( $result)) {
/*
如果物品數(shù)量>0個(gè),則逐個(gè)判斷價(jià)格并計(jì)算
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
從inventory(庫(kù)存)表中查找該物品的價(jià)格
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
總價(jià) += 該物品價(jià)格 * 該物品數(shù)量
( 大家應(yīng)該能看明白吧 )
*/
}
}
return $total; //返回總價(jià)錢(qián)
}
function display_contents( $table, $session) {
/*
獲取關(guān)于車(chē)中所有物品的詳細(xì)信息
*/
$count = 0;
/*
物品數(shù)量計(jì)數(shù)
注意,該變量不僅僅為了對(duì)物品數(shù)量進(jìn)行統(tǒng)計(jì),更重要的是,它將作為返回值數(shù)組中的下標(biāo),用來(lái)區(qū)別每一個(gè)物品!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
先取出車(chē)中所有物品
*/
while( $row = mysql_fetch_object( $result)) {
/*
分別對(duì)每一個(gè)物品進(jìn)行取詳細(xì)信息
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
從inventory(庫(kù)存)表中查找該物品的相關(guān)信息
*/
$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;
/*
把所有關(guān)于該物品的詳細(xì)信息放入 $contents數(shù)組
$contents是一個(gè)二維數(shù)組
第一組下標(biāo)是區(qū)別每個(gè)物品各個(gè)不同的信息(如物品名,價(jià)錢(qián),數(shù)量等等)
第二組下標(biāo)是區(qū)別不同的物品(這就是前面定義的 $count變量的作用)
*/
$count++; //物品數(shù)量加一(即下一個(gè)物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同時(shí)調(diào)用上面那個(gè)cart_total函數(shù),計(jì)算下總價(jià)錢(qián)
并放入 $contents數(shù)組中
*/
return $contents;
/*
將該數(shù)組返回
*/
}
function num_items( $table, $session) {
/*
返回物品種類(lèi)總數(shù)(也就是說(shuō),兩個(gè)相同的東西算一種 好像是廢話(huà)- -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
取出車(chē)中所有物品,獲取該操作影響的數(shù)據(jù)庫(kù)行數(shù),即物品總數(shù)(沒(méi)什么好說(shuō)的)
*/
}
function quant_items( $table, $session) {
/*
返回所有物品總數(shù)(也就是說(shuō),兩個(gè)相同的東西也算兩個(gè)物品 - -#)
*/
$quant = 0;// 物品總量
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
把每種物品逐個(gè)取出
*/
$quant += $row->quantity; //該物品數(shù)量加到總量里去
}
return $quant; //返回總量
}
}
您可能感興趣的文章:
- php 購(gòu)物車(chē)完整實(shí)現(xiàn)代碼
- php購(gòu)物車(chē)實(shí)現(xiàn)代碼
- php 購(gòu)物車(chē)的例子
- php網(wǎng)上商城購(gòu)物車(chē)設(shè)計(jì)代碼分享
- 深入PHP購(gòu)物車(chē)模塊功能分析(函數(shù)講解,附源碼)
- php實(shí)現(xiàn)購(gòu)物車(chē)功能(上)
- php利用cookies實(shí)現(xiàn)購(gòu)物車(chē)的方法
- php購(gòu)物車(chē)實(shí)現(xiàn)方法
- PHP實(shí)現(xiàn)的比較完善的購(gòu)物車(chē)類(lèi)
- PHP session實(shí)現(xiàn)購(gòu)物車(chē)功能
相關(guān)文章
PHP頁(yè)面跳轉(zhuǎn)實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)的方法
下面小編就為大家?guī)?lái)一篇PHP頁(yè)面跳轉(zhuǎn)實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Codeigniter購(gòu)物車(chē)類(lèi)不能添加中文的解決方法
這篇文章主要介紹了Codeigniter購(gòu)物車(chē)類(lèi)不能添加中文的解決方法,涉及底層代碼中關(guān)于中文的限制問(wèn)題,可通過(guò)修改正則匹配規(guī)則來(lái)解決,需要的朋友可以參考下2014-11-11四種php中webservice實(shí)現(xiàn)的簡(jiǎn)單架構(gòu)方法及實(shí)例
這篇文章主要介紹了四種php中webservice實(shí)現(xiàn)的簡(jiǎn)單架構(gòu)方法及實(shí)例,需要的朋友可以參考下2015-02-02Thinkphp 在api開(kāi)發(fā)中異常返回依然是html的解決方式
今天小編就為大家整理了一篇Thinkphp 在api開(kāi)發(fā)中異常返回依然是html的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10Laravel5.* 打印出執(zhí)行的sql語(yǔ)句的方法
本篇文章主要介紹了Laravel5.* 打印出執(zhí)行的sql語(yǔ)句的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07PHP實(shí)現(xiàn)微信小程序在線(xiàn)支付功能(代碼實(shí)例)
這篇文章主要介紹了PHP微信小程序在線(xiàn)支付功能(代碼實(shí)例),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03