一个购物车的原代码
一个购物车的代码,变量放在一个数组里,用session传递,
<?php
//////////接收页面变量,并放入数组
session_start();
 if(!
session_is_registered('stationery'))
  
session_register('stationery');
 
?>
<HTML>
<HEAD>
<TITLE>网上商城购物车</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<link rel=stylesheet href=site.css>
<script language="JavaScript">
setTimeout("windowclose()",3000);
    function windowclose() 
    {
        window.close();
    }
    </script>
</head><body bgcolor=#FAFBDB>

 <?php
 
if($id)
  {
  echo 
'<center><font 
color=#2C20C1><b>您所选的商品已经放入购物车!</b></font></center><br>'
;
  echo 
'<center><font color=#FF0000 size=+1><b>'.$name.'</b></font></center><br>';
 
?>

<?php
  $i
=count($stationery);
  if(
$i==0)
   {  
      
$stationery[$i]['id']=$id;
      
$stationery[$i]['name']=$name;
      
$stationery[$i]['price']=$price;
      
$stationery[$i]['unit']=$unit;
      
$stationery[$i]['num']=1;  
   }
  else
  {
      for(
$j=0;$j<$i;$j++)
        {
     if(
$stationery[$j]['id']==$id)//数组不为空,遍历
            
exit();
          }
         
$stationery[$i]['id']=$id;            //如没有记录,给数组增添一个元素
          
$stationery[$i]['name']=$name;
                       
$stationery[$i]['price']=$price;
                       
$stationery[$i]['unit']=$unit;
          
$stationery[$i]['num']=1;          //数量初始化为1
   
}
}
?>
<center>该对话框将于3秒后关闭</center>
 
  </body>
  
  </html>



<HTML>
<HEAD>
<link rel=stylesheet href=site.css>
<TITLE> 网上商城购物清单 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY bgcolor=#FAFBDB>
<?php
/////////////响应清空,购买,重计算货款,以及查看购物车的功能。
 
if($pay)
  if(!
session_is_registered('loginame'))
  {
   echo 
"<center><font color=#3333CC><b>您还没有登录.</b></font></center><br>";
   echo 
"<center><font 
color=#2D2DFF><b>如果您已是商城注册会员,请直接登录:</b></font></center>"
;
   echo 
"<FORM METHOD=POST ACTION='login.php'><table align=center width=50%><tr><td 
align=right>
         用户名</td><td align=left> <input type=text name=id size=12 
maxlength=12></td></tr>
         <tr><td align=right>密码</td><td align=left><input type=password name=pass 
size=12 maxlength=12></td></tr>
         </table><br>"
;
   echo 
"<center><input type=submit name=submit value=登录></center><br></form>";
   echo 
"<center><font color=#2D2DFF>如果您还没有成为商城会员,请在这里</font><a 
href='login.php' target=_blank><b><font 
color=#004623>注册</font></b></a><center>"
;  
  exit();
  }
 else
 {
  
$connect=odbc_connect('apolloly','sa','');
  
$cou=count($stationery);
  for(
$j=0;$j<$cou;$j++)
  {
  if(
$stationery[$j]['num']<=0)
   {
    continue;
    }
  
$current=date("Y-m-d H:i");
  
$iid=$stationery[$j]['id'];
  
$nnum=$stationery[$j]['num'];
  
$sql="insert into 
wuju_purchase(member_id,wenju_id,amount,order_date)values('$loginame','$iid','$nnum'
,'$current')"
;
  
$result=odbc_do($connect,$sql);

  
$query_hit="select hits from wenju where id='$iid'";
  
$va=odbc_do($connect,$query_hit);
  
$hit=odbc_result($va,hits);
  
$hit=$hit+1;
 
  
$updt="update wenju set hits='$hit' where id='$iid'";
  
$execute=odbc_do($connect,$updt);
  }
  unset(
$stationery);
  echo 
'尊敬的 <font color=#0000CE><b>'.$loginame.':</b></font><br>';
  echo 
'<br><br><center>&nbsp;&nbsp;您已完成本次购买,我们立即开始服务,请静侯回音.</center
><br>'
;
  echo 
'<center>如有疑问,请拨打服务电话:86-22-********</center>';
  exit();
  }
  
?>
<?php
 
if($destroy)
 {
  unset(
$stationery);
  echo 
"<center><font color=red><b>购物车已经清空,请重新选择.</b></font></center>";
  exit();
  }
 if(
$action=='get')
 {
  
$size=count($stationery);
  if(
$size==0)
   {
    echo 
'<center><font color=red><b>购物车内没有任何物品.</b></font></center>';
    exit();
    }
  if(
$size!=0)
  {
  echo 
'<FONT 
color=#5555FF><b>&nbsp;&nbsp;您在网上商城的购物清单,请查对:</b></FONT>'
;
  
?>
    
    <table width=400 align=center border=1 bordercolor=#49B7BC bgcolor=#C1C1C1 
bordercolordark=#DEEEF1 
bordercolorlight=#2E626B cellspacing=0 cellpadding=5>
    <tr><td align=center width=35%><b>商品名称</b></td><td align=center 
width=15%><b>单位</b></td><td align=center 
width=15%><b>价格</b></td> 
      <td align=center width=15%><b>数量</b></td><td  align=center  
width=20%><b>货款</b></td></tr>

   <?php
  $aa
=1;
   
$total=0;
   echo 
'<FORM METHOD=POST ACTION="'.$PHP_SELF.'?action=get">';
   for(
$s=0;$s<$size;$s++)
   {
   
$digit[$s]='value'.$aa++;
   if(isset($
$digit[$s]))
   {
    
$stationery[$s]['num']=$$digit[$s];
   }
   if($
$digit[$s]<0)
   {
    
$stationery[$s]['num']=1;
   }
   
$money=$stationery[$s]['price']*$stationery[$s]['num'];
   if(
$money==0)
   {
    continue;
    }
 echo 
'<tr><td align=center>'.$stationery[$s]['name'].'</td><td 
align=center>'
.$stationery[$s]['unit'].
'</td><td align=center>'.$stationery[$s]['price'].'</td><td align=center><input 
type=text name='
.$digit[$s].' size=3 maxlength=3 
value='
.$stationery[$s]['num'].'></td><td align=center>'.$money.'</td></tr>';
  
$total=$total+$money;
    }
 echo 
'<tr><td colspan=5 align=center>您本次购物的总货款是:人民币<font 
color=#FF0000><b>'
.$total.'</b></font>元。</td></tr>';
 echo 
'</table><br><br>';
 echo 
'<table width=300 border=0 align=center>';
 echo 
'<tr><td align=center width=80><input type=submit name=destroy 
value=清空购物车></td>'
;
 echo 
'<td align=center width=30>&nbsp;</a></td>';
 echo 
'<td align=center width=80><input type=submit name=reset 
value=重计算货款></td>'
;
 echo 
'<td align=center width=30>&nbsp;</a></td>';
 echo 
'<td align=center width=80><input type=submit name=pay 
value=已决定购买></td></tr>'
;
 echo 
'</table></form><br><br>';
 }
 }
echo 
'<center><font 
color=#4A68B5><b>*注:如果您打算在商品清单中取消某样商品,请在数量栏内置"0",点击 " 
重计算货款"后,该商品即被删除.</b></font></center>'
;
echo 
'<center><font 
color=#4A68B5><b>*注:当您按下"已决定购买",本次购买行为即生效,请细心操作.</b></font>
 </center>'
;
 
?>
  
</BODY>
</HTML>


以上代码如要应用,要注意其中接收了表单的几个变量,仔细看看就明白了。