php 购物车实例(申精)

2019-04-10 10:47:39于丽

*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
修改物品数量(表名,session,物品,数量)
*/
$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)) {
/*
如果物品数量>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);
/*
总价 += 该物品价格 * 该物品数量
( 大家应该能看明白吧 )
*/
}
}
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);
/*
从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数组
$contents是一个二维数组
第一组下标是区别每个物品各个不同的信息(如物品名,价钱,数量等等)
第二组下标是区别不同的物品(这就是前面定义的 $count变量的作用)
*/
$count++; //物品数量加一(即下一个物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同时调用上面那个cart_total函数,计算下总价钱
相关文章 大家在看