> php教程 > php手册 > 一个用session实现微型购物篮的例子

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

WBOY
풀어 주다: 2016-06-21 09:13:05
원래의
1279명이 탐색했습니다.

session

采于www.phpbuilder.com,稍作补充。代码如下。嘻嘻:)
basket.php:
include ("evert.inc");
?>

<br>ID    Name    Price<br>1     Mouse   25.00 <a href="<?echo%20%24PHP_SELF;%20?>?act=add&id=1&price=25&basket=Mouse">add</a>  <a href="<?echo%20%24PHP_SELF;%20?>?act=subtract&id=1&price=25&basket=Mouse">subtract</a><br>2     Key    100.00 <a href="<?echo%20%24PHP_SELF;%20?>?act=add&id=2&price=100&basket=Key">add</a>  <a href="<?echo%20%24PHP_SELF;%20?>?act=subtract&id=2&price=100&basket=Key">subtract</a><br>3     Car   5000.00 <a href="<?echo%20%24PHP_SELF;%20?>?act=add&id=3&price=5000&basket=Car">add</a>  <a href="<?echo%20%24PHP_SELF;%20?>?act=subtract&id=3&price=5000&basket=Car">subtract</a><br>4     Game    25.00 <a href="<?echo%20%24PHP_SELF;%20?>?act=add&id=4&price=25&basket=Game">add</a>  <a href="<?echo%20%24PHP_SELF;%20?>?act=subtract&id=4&price=25&basket=Game">subtract</a><br>
로그인 후 복사

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);
}

?>



관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿