您的位置:首页 > 编程语言 > PHP开发

php简易购物车

2010-10-19 22:41 183 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: