php简易购物车

<?php  
session_start();
include 'global.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>购物车</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="./css/styles.css" type="text/css" />

</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"
 text="#000000" link="#000000" vlink="#000000" alink="#000000">
<center><div><h2>欢迎<?php echo $_SESSION['uname'];?></h2></div></center>
<table width="100%" border="0" cellspacing="0" cellpadding="0"
 bgcolor="#FFFFFF" height="100%">
 <tr valign="top">
  <td>
  <table width="98%" border="0" cellspacing="1" cellpadding="2"
   align="center">
   <tr valign="bottom">
    <td height="30"><img
     src="http://i-560.wz8.cn/pic1/shop/cart.gif"> <font
     color="#000000">您的购物车中有以下商品</font></td>
   </tr>
  </table>
  <table width="98%" border="0" cellspacing="2" cellpadding="0"
   align="center">
   <tr bgcolor=#808000>
    <td height="1" bgcolor="#999999"></td>
   </tr>
  </table>
  <table width="98%" border="0" cellspacing="2" cellpadding="0"
   align="center">
   <tr>
    <td height="5"></td>
   </tr>
  </table>
  <table width="98%" border="0" cellspacing="0" cellpadding="0"
   align="center">
   <tr>
    <td>
    <table width="100%" border="0" align="CENTER" cellpadding="2"
     cellspacing="1" bgcolor="#c0c0c0">
     <tr bgcolor="#dadada">
      <td height="22" width="50">
      <div align="CENTER"><font color="#000000">编号</font></div>
      </td>
      <td width="610" height="22">
      <div align="CENTER"><font color="#000000">商品名称</font></div>
      </td>
      <td height="22" width="104">
      <div align="CENTER"><font color="#000000">单价</font></div>
      </td>
      <td height="22" width="100">
      <div align="CENTER"><font color="#000000">数量</font></div>
      </td>
      <td width="116" height="22">
      <div align="CENTER"><font color="#000000">金额</font></div>
      </td>
     </tr>
      <?php
      include 'dbManager.php';
                    $id=strval($_GET["id"]); //获取变量的字符串值
                    $tatalprice=0;//总价钱
                    //清空购物车
                    if($_GET["qk"]=="yes")
                    {
                    $_SESSION["car"]=null;
                    }else {
           //判断购物车是否存在
          if($_SESSION["car"]==null)
           {
           //如果为空,创建购物车
           $_SESSION["car"]=array();
         }
         //取出购物车
      $car=$_SESSION["car"];
    //判断购买的商品是否存在
    if(!array_key_exists($id, $car))
    {
     //不存在
         $dbmanager=new DBManager();
         $sql="select foodid,foodname,foodprice from foodinfo where foodId=".$id;
         $data=$dbmanager->select($sql); 
       $car[$id]["foodname"]=$data[0][1];
        $car[$id]["foodprice"]=$data[0][2];
        $car[$id]["quantity"]=1; 
    }
    else
    {
     //存在数量加一
     $car[$id]["quantity"]+=1; 
    }
    //更新session
    $_SESSION["car"]=$car;
    foreach ($car as $key=>$value) {
   ?>
     <tr bgcolor="#ffffff">
      <td width="50" align="center" height="22"><font
       color="#000000"><?php echo $key;?></font></td>
      <td align="center" height="22"><font color="#000000"><?php echo $value["foodname"];?></font>
      <input type="hidden" name="prodid" value="500047"></td>
      <td width="104" align="center" height="22"><font
       color="#000000">¥<?php echo $value["foodprice"];?></font></td>
      <td width="100" class="hh" align="center" height="22"><?php echo $value["quantity"];?></td>
      <td width="116" class="bb" align="center" height="22"><font
       color="#000000">¥<?php echo ($value["quantity"]*$value["foodprice"])."元";?></font></td>
     </tr>
          <?php
         $tatalprice+=($value["quantity"]*$value["foodprice"]);
          ?>
                     <?php }?>
                    <tr bgcolor="#dadada">
      <td width="50" height="22" align="center"><font
       color="#000000">合计</font></td>
      <td height="22" align="center"><font color="#000000">-</font></td>
      <td width="104" height="22" align="center"><font
       color="#000000">-</font></td>
      <td width="100" class="hh" height="22" align="center"><font
       color="#000000">-</font></td>
      <td width="116" class="bb" align="center" height="22"><font
       color="#000000">¥<?php echo $tatalprice;?> </font></td>
     </tr>
     <?php }?>
    </table>
    <br>
    <table width="300" border="0" cellspacing="1" cellpadding="4"
     align="CENTER" bgcolor="#c0c0c0">
     <tr bgcolor="#dadada">
      <td height="10" align="center"><a href="shopCart.php?qk=yes "><font
       color="#000000">清空购物车</font></a></td>
      <td height="10" align="center" style="cursor:hand" onClick="#"><a href="show.php"><font
       color="#000000">继续购物</font></a></td>
      <td height="10" align="center" style="cursor:hand" onClick="#"><a href="checkOut.html"><font
       color="#000000">生成订单</font></a></td>
     </tr>
    </table>
    </td>
   </tr>
  </table>
  </td>
 </tr>
</table>
<br>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值