·您的位置: 首页 » 资源教程 » 编程开发 » PHP » 一个用session实现微型购物篮的例子

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

类别: PHP教程  评论数:0 总得分:0
采于www.phpbuilder.com,稍作补充。代码如下。嘻嘻:)
basket.php:
<?php
include ("evert.inc");
?>
<PRE>
ID    Name    Price
1     Mouse   25.00 <A HREF="<?echo $PHP_SELF; ?>?act=add&id=1&price=25&basket=Mouse">add</A>  <A HREF="<?echo $PHP_SELF; ?>?act=subtract&id=1&price=25&basket=Mouse">subtract</A>
2     Key    100.00 <A HREF="<?echo $PHP_SELF; ?>?act=add&id=2&price=100&basket=Key">add</A>  <A HREF="<?echo $PHP_SELF; ?>?act=subtract&id=2&price=100&basket=Key">subtract</A>
3     Car   5000.00 <A HREF="<?echo $PHP_SELF; ?>?act=add&id=3&price=5000&basket=Car">add</A>  <A HREF="<?echo $PHP_SELF; ?>?act=subtract&id=3&price=5000&basket=Car">subtract</A>
4     Game    25.00 <A HREF="<?echo $PHP_SELF; ?>?act=add&id=4&price=25&basket=Game">add</A>  <A HREF="<?echo $PHP_SELF; ?>?act=subtract&id=4&price=25&basket=Game">subtract</A>
</PRE>
<a href="<?echo $PHP_SELF;?>?logout=1">Logout</a>


evert.inc:
<?php
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]<0)
                       $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<$ses_basket_items;$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 "<BR>/n";
      }
   }
} else {
        $ses_basket_items=0;
        unset($ses_basket_name);
        unset($ses_basket_amount);
        unset($ses_basket_price);
        unset($ses_basket_id);
}

?>




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1