objcart.php
<?php
//All rights reserved;
class Cart {
  var $items ;  

function destroy(){
unset($this); 
}

function add_goodsnum($fnum) {
if ($this->items[num]+round($fnum) >= 0){
$this->items[num] +=round($fnum);
if ($this->items[num]==0) {unset($this); };
return true;
}
else{
return false;
}
}

function setgoods ($fgoodsname, $fnum,$fprice,$fdescript) {
    if ($fnum<0){ return false;}
else {
if ($fnum ==0)
{
unset($this);
}
else{
$this->items[goodsname]= $fgoodsname;
$this->items[num] =round($fnum);
$this->items[price]=$fprice;
$this->items[descript]=$fdescript;
}
}
  } 
  
  function remove_goodsnum ($fgoodsname, $fnum) {
    if ($this->items[num] > $fnum) {
      $this->items[num] -= $fnum;
      return true;
    } 
else {
      return false;
    }   
  }
}
?>

解决方案 »

  1.   

    <?php
    include("objcart.php");
    ob_start();                                                  
    session_start();
    if (!session_is_registered("basket")){
     $basket=new Cart;
    session_register("basket");}
    switch ($actions) {
    case "view":
    break; //?????!
    case "clear":
    /********************************************/
    $basket=null;//???????
    session_unregister("basket");
    echo "<script language=javascript> window.close()</script>";
    break;
    /********************************************/
    case "remove":
    //###########################################/
    if (isset($serial)){
    $l=count($basket); for ($i=$serial;$i<$l;$i++){//????????????????????
    $basket[$i]=$basket[$i+1];
      };
    $basket=array_slice($basket,0,$l-1);// ?????????????!
    reset($basket);};
    break;
    //********************************************* case "update":
    $i=count($basket);
    for ($j=0;$j<$i;$j++){
      if ($basket[$j]->items[goodsname]==$psecode){
     $basket[$j]->setgoods($psecode,$fnum,$pseprice,$itemname);
     break;
    }
      }
    break;
    //*********************************************

    default:
    ////////////////////??????? ????????/////////////////////////////////
    $i=count($basket);
    if ($psecode!="") {
    if ($i==0 ){ 
    $basket[$i]=new Cart;
    $basket[$i]->setgoods($psecode,1,$pseprice,$itemname);
    reset($basket);
      }else{
     for($j=0;$j<$i;$j++){
    if ($basket[$j]->items[goodsname]==$psecode){
    break;
      }else{
    if ($j==($i-1)){
    $basket[$i]=new Cart;
    $basket[$i]->setgoods($psecode,1,$pseprice,$itemname);
      };
    };
    };
    reset($basket);
    };
      }; 
      break; };//switch ??!;  //?????,??????????????????,??????????????
    if (isset($basket)){reset($basket);};
    $a=count($basket);
    for ($k=0;$k<$a;$k++){ 
      if ($basket[$k]==null){
    for ($x=$k;$x<$a;$x++){
    $basket[$x]=$basket[$x+1];
      };
    $basket=array_slice($basket,0,$a-1);
    reset($basket);
    break;
       };
    };
    ?>
    <br>
    <div class="pageHeading">What's In My Cart?</div>
    <br>
    <!--Website Under Maintaining, We won't accept any order at this time. 
    Any question, Please feel free to contact us: [email protected]
    Thanks you!-->
    <br>
    <div class="color_2">IMPORTANT!<br>
      If you change quantity of the products you want to order in the &quot;Qty&quot; window, make sure to click the &quot;Update Cart&quot; button on the right side of the shopping table. </div>
    <TABLE width="98%" 
                border=0 align="center" cellPadding=2 cellSpacing=0 class=productListing>
      <TBODY>
        <TR align="center">
          <TD width="15%" class=infoBoxHeading>&nbsp;&nbsp;<STRONG>Product ID</STRONG></TD>
          <TD width="31%" class=infoBoxHeading>&nbsp;<STRONG>Description</STRONG>&nbsp;</TD>
          <TD width="8%" class=infoBoxHeading>&nbsp;<STRONG>Qty </STRONG>&nbsp;</TD>
          <TD width="11%" class=infoBoxHeading><STRONG>Price</STRONG></TD>
          <TD width="12%" class=infoBoxHeading><STRONG>Subtotal</STRONG></TD>
          <TD width="11%" class=infoBoxHeading><STRONG>Update</STRONG></TD>
          <TD width="12%" class=infoBoxHeading><STRONG>Delete</STRONG></TD>
        </TR>
        <?php
    $alltotal=0;
    echo $a=count($basket);
    for ($k=0;$k<$a;$k++)
    {
    ?>
      <form name=form<?php echo $k?> method = post action=<?php echo $PHP_SELF?>?actions=update>
        <TR class=<?php if($i%2==0){ echo "productListing-odd";}else{ echo "productListing-even";}?>>
          <TD class=productListing-data align=middle><div align="center"><span class="font"><font color=blue>
              <input type =hidden readonly=true name=psecode value='<?php echo $basket[$k]->items[goodsname]?>'>
              <?php echo $basket[$k]->items[goodsname]?></font></span></div></TD>
          <TD class=productListing-data><span class="infoBoxHeadingcart">
            <input type =hidden readonly =true name =itemname value='<?php echo $basket[$k]->items[descript] ?>'>
            <?php echo $basket[$k]->items[descript] ?></span></TD>
          <TD align=middle class=productListing-data><div align="center"><span class="font">
              <input name =fnum type=text  value=<?php echo $basket[$k]->items[num] ?> size="3" maxlength="3">
          </span></div></TD>
          <TD align=middle class=productListing-data><div align="center"><span class="list_price_price">
              <input name=newpse type =hidden value=<?php  echo $basket[$k]->items[price] ?> size="10" readonly =true>
              <span class="text_price_total">&pound;</span><?php echo $basket[$k]->items[price] ?></span></div></TD>
          <TD align=middle class=productListing-data><div align="center"><span class="list_price_price"><font color=red>
              <?php
    $total_b=(double)($basket[$k]->items[num] * $basket[$k]->items[price]);
    //print "\$$total_b";
    echo "\$" . number_format($total_b,2,'.','');
      ?>
          </font></span></div></TD>
          <TD align=middle class=productListing-data><div align="center">
              <input type="image" align="middle" border="0" src="images/update.gif" alt="update cart">
              <input type="hidden" name="sitename" value="<?php echo $websitename;?>">
              <input type="hidden" name="balance" value='<?php echo $basket[$k]->items[balance] ;?>'>
          </div></TD>
          <TD align=middle class=productListing-data><div align="center"><a href="cart.php?actions=remove&sitename=<?php echo $websitename?>&serial=<?php echo $k?>"><img src="images/delete.gif" border="0" alt="delete"></a></div></TD>
        </TR>
      </form>
      <?php 
         $alltotal+=$total_b ;
          };    $shipp=7;
          $alltotal+=$shipp;
       ?>
    </TABLE>
    <table onload="juger_cookie()" border=0 width=100% align=center bordercolor="#666699" borderlightcolor="#666699" borderdarkcolor="#666699" cellspacing="0" cellpadding="0" bordercolorlight="#666699" bordercolordark="#BFBFD5">
      <tr>
        <td width="36%" rowspan="2"  align="center"><a href='cart.php?actions=clear&sitename=<?php echo $websitename ;?>'><img align="middle" border="0" src="images/clear_cart.gif" alt="Clear Cart"></a></td>
        <td width="35%" rowspan="2" align="center"><a href="#" onClick="javascript:window.close()"><img align="middle"  border="0" src="images/continue.gif" alt="Continue"></a>&nbsp;</td>
      </tr>
      <tr bgcolor="#FFFFFF">
        <td height="48" align="right" class="font"><font color=#565787 size=2><b>Amount:</b></font> </td>
        <td colspan="2" class="text_price_total">UK &pound;<?php echo number_format($alltotal-$shipp,2,'.','');?> </td>
      </tr>
      <form name="formcheck" method ="post" action="getinfo.php">
        <tr>
          <td height="56" colspan="2" align="center" class="font"><a href="#" onClick="window.open('./battery_shipping.htm','battery_shipping','left=180,top=100,resizable=no,scrollbars=no,status=no,width=580,height=320')">Please click here about the shipping and handling.</a>
              <input type="hidden" name="totall" value='<?php echo base64_encode($alltotal) ;?>'>
              <input type="hidden" name="webname" value='<?php echo $websitename;?>'>
              <input type="hidden" name="itemname" value='<?php echo $basket[0]->items[descript];?>'>
              <input type="hidden" name="SESS" value='<?php echo session_id();?>'>
          </td>
          <td height="36" colspan="5" align="center"><input name="image" type=image value=check src="images/check_out.gif" border="0" alt="Check Out"></td>
        </tr>
      </form>
    </table>