购物车浏览商品界面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" href="888.css" type="text/css" />
<script>
function test()
{
if(!confirm('确认购买吗?'))
return false;
}
</script>
</head>
<body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><a href="888_ok.php">浏览图书</a></li>
<li><a href="shop888.php">查看购物车</a></li>
<li><a href="clearshop.php">清空购物车</a></li>
</ul>
</div>
<div id="show">
<table width="90%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5%" valign="middle">
<td width="5%" align="center">id</td>
<td width="20%" align="center">书名</td>
<td width="10%" align="center">价格</td>
<td width="30%" align="center">出版日期</td>
<td width="10%" align="center">类型</td>
</tr>
<?php
include_once("../conn/conn.php");
$sqlstr="select * from tb_demo01 order by id";
$result=mysql_query($sqlstr,$conn);
while($rows=mysql_fetch_array($result))
{
echo"<tr><td align='left' height='25'> ";
echo "<input type=checkbox name='chk[]' id='chk' value=".$rows[0].">";
echo"</td>";
for($i=0;$i<count($rows);$i++)
{
echo"<td align='center' height='25'>".$rows[$i]."</td>";
}
echo"<td><a href='addshop11.php?id={$rows[0]}' onclick='return test()'>加入购物车</a></td>";
}
?>
</div>
</body>
</html>
点击加入购物车按钮后进入此页面,获取id进行传值
<?php
session_start();//启动会话
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" href="shop888.css" type="text/css" />
</head>
<body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><h3>添加商品到购物车,返回到<a href="shop888.php">我的购物车</a></h3></li>
</ul>
</div>
<div id="show">
<?php
include_once("../conn/conn.php");
$sql="select * from tb_demo01 where id={$_GET['id']}";
$result=mysql_query($sql,$conn);
if(empty($result)||mysql_num_rows($result)==0)
{
die("没有找到要购买的信息");
}
else
{
$shop=mysql_fetch_array($result);
}
$shop["num"]=1; //添加一个数量的字段
//var_dump($shop);
//如果存在则实现数量的累加,如果不存在则不累加
if(isset($_SESSION["shoplist"][$shop['id']]))
{
//如果存在,数量++
$_SESSION["shoplist"][$shop['id']]['num']++;
}
else
//如果不存在
{
$_SESSION["shoplist"][$shop['id']]=$shop;
}
?>
</div>
</body>
</html>
购物车显示页面
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" href="shop888.css" type="text/css" />
<html>
<head></head>
<body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><h3>我的购物车,返回商城<a href="888_ok.php">继续购买</a></h3></li>
</ul>
</div>
<div id="show">
<table border="1" width=90% cellpadding="0" cellspacing="0">
<tr>
<th>id</th>
<th>书名</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>日期</th>
<th>类型</th>
</tr>
<?php
$sum=0;
if(isset($_SESSION["shoplist"]))
{
foreach($_SESSION["shoplist"] as $value)
{
echo"<tr>";
echo"<td>{$value[0]}</td>";
echo"<td>{$value[1]}</td>";
echo"<td>{$value[2]}</td>";
echo"<td>
<button onclick='window.location.href='updateshop.php?id=$value[0]&num=-1''>-</button>
{$value['num']}
<button onclick='window.location.href='updateshop.php?id=$value[0]&num=1''>+</button>
</td>";//数量
echo"<td>".$value[2]*$value['num']."</td>";
echo"<td>{$value[3]}</td>";
echo"<td>{$value[4]}</td>";
echo"<td><a href='clearshop.php?id=$value[0]'>删除</a></td>";
echo"</tr>";
$sum+=$value[2]*$value['num'];
}
}
?>
<tr>
<th>总计金额:</th>
<th colspan="6" align="right"><?php echo $sum; ?></th>
<td></td>
</tr>
</table>
</div>
</body>
</html>
购物车清空页面
<?php
session_start();//启动会话
//清空session中的商品
if($_GET['id'])
{
unset($_SESSION["shoplist"][$_GET['id']]);
}
else
{
unset($_SESSION["shoplist"]);
}
//跳转到购物车界面
header("Location:shop888.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>清空购物车</title>
</head>
<body>
</body>
</html>