php 購物車的例子
更新時間:2009年05月04日 23:04:28 作者:
網(wǎng)上搜到的,簡單容易理解。cookie存購物車ID,db存購物車數(shù)據(jù)。
//購物車session的產(chǎn)生代碼
if(! $session && ! $scid) {
/*
session用來區(qū)別每一個購物車,相當于每個車的身份證號;
scid只用來標識一個購物車id號,可以看做是每個車的名字;
當該購物車的id和session值兩者都不存在時,就產(chǎn)生一個新購物車
*/
$session = md5(uniqid(rand()));
/*
產(chǎn)生一個唯一的購物車session號
rand()先產(chǎn)生個隨機數(shù),uniqid()再在該隨機數(shù)的基礎上產(chǎn)生一個獨一無二的字符串,最后對該字符串進行md5
*/
SetCookie(scid, $session, time() + 14400);
/*
設置該購物車cookie
變量名:scid(不知到這里是不是少了一個 $號呢?=》更正:scid要加“”)
變量值: $session
有效時間:當前時間+14400秒(4小時內(nèi))
關于setcookie函數(shù)的詳細用法,大家還是參看php手冊吧~
*/
}
class Cart { //開始購物車類
function check_item( $table, $session, $product) {
/*
查驗物品(表名,session,物品)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
看一看'表'里該'購物車'中有沒有該'產(chǎn)品'
即,該產(chǎn)品有沒有已經(jīng)放入購物車
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
查詢失敗
*/
$numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
若沒有找到,則返回0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
若找到,則返回該物品數(shù)量
這里有必要解釋一下mysql_fetch_object函數(shù)(下面還會用到):
【mysql_fetch_object() 和 mysql_fetch_array() 類似,只有一點區(qū)別 - 返回一個對象而不是數(shù)組。】
上面這句話摘自php手冊,說得應該很明白了吧~
簡單的說就是,取一條記錄中的某個字段,應該用“->”而不是像數(shù)組一樣用下標
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
添加新物品(表名,session,物品,數(shù)量)
*/
$qty = $this->check_item( $table, $session, $product);
/*
調(diào)用上面那個函數(shù),先檢查該類物品有沒有已經(jīng)放入車中
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*若車中沒有,則像車中添加該物品*/
} else {
$quantity += $qty; //若有,則在原有基礎上增加數(shù)量
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
并修改數(shù)據(jù)庫
*/
}
}
function delete_item( $table, $session, $product) {
/*
刪除物品(表名,session,物品)
*/
$query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
mysql_query( $query);
/*
刪除該購物車中該類物品
*/
}
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) {
/*
清空購物車(沒什么好說)
*/
$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)) {
/*
如果物品數(shù)量>0個,則逐個判斷價格并計算
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
從inventory(庫存)表中查找該物品的價格
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
總價 += 該物品價格 * 該物品數(shù)量
( 大家應該能看明白吧:) )
*/
}
}
return $total; //返回總價錢
}
function display_contents( $table, $session) {
/*
獲取關于車中所有物品的詳細信息
*/
$count = 0;
/*
物品數(shù)量計數(shù)
注意,該變量不僅僅為了對物品數(shù)量進行統(tǒng)計,更重要的是,它將作為返回值數(shù)組中的下標,用來區(qū)別每一個物品!
*/
$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);
/*
從inventory(庫存)表中查找該物品的相關信息
*/
$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;
/*
把所有關于該物品的詳細信息放入 $contents數(shù)組
$contents是一個二維數(shù)組
第一組下標是區(qū)別每個物品各個不同的信息(如物品名,價錢,數(shù)量等等)
第二組下標是區(qū)別不同的物品(這就是前面定義的 $count變量的作用)
*/
$count++; //物品數(shù)量加一(即下一個物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同時調(diào)用上面那個cart_total函數(shù),計算下總價錢
并放入 $contents數(shù)組中
*/
return $contents;
/*
將該數(shù)組返回
*/
}
function num_items( $table, $session) {
/*
返回物品種類總數(shù)(也就是說,兩個相同的東西算一種 好像是廢話- -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
取出車中所有物品,獲取該操作影響的數(shù)據(jù)庫行數(shù),即物品總數(shù)(沒什么好說的)
*/
}
function quant_items( $table, $session) {
/*
返回所有物品總數(shù)(也就是說,兩個相同的東西也算兩個物品 - -#)
*/
$quant = 0;// 物品總量
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
把每種物品逐個取出
*/
$quant += $row->quantity; //該物品數(shù)量加到總量里去
}
return $quant; //返回總量
}
}
if(! $session && ! $scid) {
/*
session用來區(qū)別每一個購物車,相當于每個車的身份證號;
scid只用來標識一個購物車id號,可以看做是每個車的名字;
當該購物車的id和session值兩者都不存在時,就產(chǎn)生一個新購物車
*/
$session = md5(uniqid(rand()));
/*
產(chǎn)生一個唯一的購物車session號
rand()先產(chǎn)生個隨機數(shù),uniqid()再在該隨機數(shù)的基礎上產(chǎn)生一個獨一無二的字符串,最后對該字符串進行md5
*/
SetCookie(scid, $session, time() + 14400);
/*
設置該購物車cookie
變量名:scid(不知到這里是不是少了一個 $號呢?=》更正:scid要加“”)
變量值: $session
有效時間:當前時間+14400秒(4小時內(nèi))
關于setcookie函數(shù)的詳細用法,大家還是參看php手冊吧~
*/
}
class Cart { //開始購物車類
function check_item( $table, $session, $product) {
/*
查驗物品(表名,session,物品)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
看一看'表'里該'購物車'中有沒有該'產(chǎn)品'
即,該產(chǎn)品有沒有已經(jīng)放入購物車
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
查詢失敗
*/
$numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
若沒有找到,則返回0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
若找到,則返回該物品數(shù)量
這里有必要解釋一下mysql_fetch_object函數(shù)(下面還會用到):
【mysql_fetch_object() 和 mysql_fetch_array() 類似,只有一點區(qū)別 - 返回一個對象而不是數(shù)組。】
上面這句話摘自php手冊,說得應該很明白了吧~
簡單的說就是,取一條記錄中的某個字段,應該用“->”而不是像數(shù)組一樣用下標
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
添加新物品(表名,session,物品,數(shù)量)
*/
$qty = $this->check_item( $table, $session, $product);
/*
調(diào)用上面那個函數(shù),先檢查該類物品有沒有已經(jīng)放入車中
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*若車中沒有,則像車中添加該物品*/
} else {
$quantity += $qty; //若有,則在原有基礎上增加數(shù)量
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
并修改數(shù)據(jù)庫
*/
}
}
function delete_item( $table, $session, $product) {
/*
刪除物品(表名,session,物品)
*/
$query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
mysql_query( $query);
/*
刪除該購物車中該類物品
*/
}
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) {
/*
清空購物車(沒什么好說)
*/
$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)) {
/*
如果物品數(shù)量>0個,則逐個判斷價格并計算
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
從inventory(庫存)表中查找該物品的價格
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
總價 += 該物品價格 * 該物品數(shù)量
( 大家應該能看明白吧:) )
*/
}
}
return $total; //返回總價錢
}
function display_contents( $table, $session) {
/*
獲取關于車中所有物品的詳細信息
*/
$count = 0;
/*
物品數(shù)量計數(shù)
注意,該變量不僅僅為了對物品數(shù)量進行統(tǒng)計,更重要的是,它將作為返回值數(shù)組中的下標,用來區(qū)別每一個物品!
*/
$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);
/*
從inventory(庫存)表中查找該物品的相關信息
*/
$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;
/*
把所有關于該物品的詳細信息放入 $contents數(shù)組
$contents是一個二維數(shù)組
第一組下標是區(qū)別每個物品各個不同的信息(如物品名,價錢,數(shù)量等等)
第二組下標是區(qū)別不同的物品(這就是前面定義的 $count變量的作用)
*/
$count++; //物品數(shù)量加一(即下一個物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同時調(diào)用上面那個cart_total函數(shù),計算下總價錢
并放入 $contents數(shù)組中
*/
return $contents;
/*
將該數(shù)組返回
*/
}
function num_items( $table, $session) {
/*
返回物品種類總數(shù)(也就是說,兩個相同的東西算一種 好像是廢話- -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
取出車中所有物品,獲取該操作影響的數(shù)據(jù)庫行數(shù),即物品總數(shù)(沒什么好說的)
*/
}
function quant_items( $table, $session) {
/*
返回所有物品總數(shù)(也就是說,兩個相同的東西也算兩個物品 - -#)
*/
$quant = 0;// 物品總量
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
把每種物品逐個取出
*/
$quant += $row->quantity; //該物品數(shù)量加到總量里去
}
return $quant; //返回總量
}
}
相關文章
php redis 處理websocket聊天記錄的實例代碼
本文通過實例代碼給大家介紹了php redis 處理websocket聊天記錄的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07