时间:2021-05-26
//购物车session的产生代码
if(! $session && ! $scid) {
$session = md5(uniqid(rand()));
SetCookie(scid, $session, time() + 14400);
}
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; //返回总量
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了PHP实现的购物车类。分享给大家供大家参考。具体分析如下:该购物车类是基于CodeIgniter的购物车类仿写实现的。购物车基本功能如下:1)将物
本文实例讲述了php利用cookies实现购物车的方法。分享给大家供大家参考。具体分析如下:php购物车是在电子商务网站会用到的,一种像超市购物车一样的,选好商
本文实例讲述了php实现仿写CodeIgniter的购物车类。分享给大家供大家参考。具体如下:这里仿写CodeIgniter的购物车类购物车基本功能:1)将物品
PHP+Mysql简单实现了图书购物车本文主要讲述如何通过PHP+HTML简单实现图书购物车的功能,这是提取我们php项目的部分内容。主要内容包括:1.通过Ja
本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下:之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加