欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个用session实现微型购物篮的例子_PHP

程序员文章站 2022-05-24 11:38:03
...
采于www.phpbuilder.com,稍作补充。代码如下。嘻嘻:)
basket.php:
include ("evert.inc");
?>

ID Name Price
1 Mouse 25.00 add subtract
2 Key 100.00 add subtract
3 Car 5000.00 add subtract
4 Game 25.00 add subtract

Logout


evert.inc:
session_start();
if($logout){
session_destroy();
unset($ses_basket_name);
unset($ses_basket_amount);
unset($ses_basket_price);
unset($ses_basket_id);
unset($ses_basket_items);
}
if ($basket!=""){
if (session_is_registered("ses_basket_items")){
$basket_position_counter=0;
$double=0;

if ($ses_basket_items>0){
foreach ($ses_basket_name as $basket_item){
if ($basket_item==$basket){
$double=1;
$basket_position=$basket_position_counter;

}
$basket_position_counter++;
}
}
if ($double==1){
$oldamount=$ses_basket_amount[$basket_position];
if($act=="add")
$ses_basket_amount[$basket_position]++;
else{
$ses_basket_amount[$basket_position]--;
if($ses_basket_amount[$basket_position] $ses_basket_amount[$basket_position]=0;
}
$amount=$ses_basket_amount[$basket_position];
$oldprice=$ses_basket_price[$basket_position];
$newprice=$oldamount>0?($oldprice/$oldamount)*$amount:$price;
$ses_basket_price[$basket_position]=$newprice;
}
else{
$ses_basket_name[]=$basket;
$ses_basket_amount[]=1;
$ses_basket_price[]=$price;
$ses_basket_id[]=$id;
$ses_basket_items++;
}
}else{
$ses_basket_items=1;
$ses_basket_name[0]=$basket;
$ses_basket_amount[0]=1;
$ses_basket_price[0]=$price;
$ses_basket_id[0]=$id;
session_register("ses_basket_items");
session_register("ses_basket_name");
session_register("ses_basket_amount");
session_register("ses_basket_price");
session_register("ses_basket_id");
}
}

if ($ses_basket_items>0){
for ($basket_counter=0;$basket_counter $price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
$amount=$ses_basket_amount[$basket_counter];
$name=$ses_basket_name[$basket_counter];
if($amount>0){
echo "$amount $name $price";
echo "
\n";
}
}
} else {
$ses_basket_items=0;
unset($ses_basket_name);
unset($ses_basket_amount);
unset($ses_basket_price);
unset($ses_basket_id);
}

?>