现在的位置: 首页 > php学习 > 正文

php购物车模块的一个购物车单页面

2014年11月17日 php学习 ⁄ 共 3652字 ⁄ 字号 暂无评论 ⁄ 阅读 15,801 次

php购物车模块的一个购物车单页面,内容有些繁琐,需要删除重新做,舍不得直接保存一下.

car.php代码如下:

<?php
include(dirname(__FILE__).'/init.php');
session_start();

//$sessionId = session_id();

//显示当前登陆用户

// 参数为out,注销
if($_GET['log'] == "out"){
//setcookie("username","",time()-1,"/");
//setcookie("userid","",time()-1,"/");
unset($_SESSION["username"]);
unset($_SESSION["userid"]);
$_GET['log'] =null;
echo"<script>history.go(-1);</script>";
$userid = null;
}

$userid=$_SESSION['userid'];
$name=$_SESSION['username'];

var_dump($_SESSION['username']);
var_dump($_SESSION['userid']);
$good_id =$_GET['id'];
//var_dump($good_id);

?>

<!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>
<table width="649" height="273" border="1">
<tr>
<td width="132">商品名称</td>
<td width="117">属性</td>
<td width="378">价格</td>

<td width="378">&nbsp;</td>
<td width="378">数量</td>
<td width="378">&nbsp;</td>
<td width="378">小计</td>
<td width="378">&nbsp;</td>
</tr>
<?php

var_dump($userid);
if(isset($userid))
{
//如果用户已登录
//输出登陆状态
echo"当前登陆用户:".$name."<a href = './car.php?log=out'>注销</a>";

//记录进数据库的用户id
$inuserid = $userid;
unset($userid);
}else
{
$sessionid =    session_id();
//如果是未登录用户
echo "游客--<a href='./log.php'>登陆</a> 当前sessionid为".$sessionid ;
//记录进数据库的用户id
$inuserid = $sessionid;
}
echo "-----<a href='./car.php'>查看购物车</a>-----<a href = './shop.php'>继续购物</a>";
//var_dump($inuserid);

//判断是否是添加产品至购物车,还是查看购物车,

if(isset($good_id)){
//添加产品至购物车
//    var_dump($inuserid );
//判断购物车内是否有该产品
$sql = "select count(*) from car where userid = '$inuserid' and good_id = '$good_id'";
$rs = $con->query($sql);
$row = $rs->fetch_array();
$num = $row[0];
if($num==0)
{
//增加新的数据
$sql = "insert into car(userid,num,good_id) values('$inuserid','1','$good_id') ";
$rs = $con->query($sql);
}else
{

//查看当前数量
$sql = "select num from car where userid ='$inuserid' and good_id ='$good_id'";
$rs = $con->query($sql);
$row = $rs->fetch_assoc();
$num = $row['num'] +1;
//var_dump($row['num']);
//var_dump($good_id);
//var_dump($num);
//更新数量

$sql = "update car set num='$num' where userid= '$inuserid' and good_id ='$good_id'";
$rs = $con->query($sql);
}

///输出当前购物车产品
//$sql = "select * from car where userid ='$inuserid'";

//select tableid,name1,name2 from 表1as AA left join 表2 as BB  on AA.AName=BB.id and AA.BName=BB.id

$sql = "select car.good_id,car.num,good.price,good.title,good.content  from car left join good on car.good_id = good.id where car.userid = '$inuserid' ";
$rs = $con->query($sql);

while ($row = $rs->fetch_assoc())
{
echo " <tr>

<td>".$row['title']."</td>
<td>".$row['content']."</td>
<td>".$row['price']."</td>
<td><a href ='./cardel.php?id=".$row['good_id']."&del=1'>减一</a></td>
<td>".$row['num']."</td>
<td><a href ='./cardel.php?id=".$row['good_id']."&del=2'>加1</a></td>
<td>".$row['price']*$row['num']."</td>

<td><a href ='./cardel.php?id=".$row['good_id']."&del=del'>删除</a></td>
</tr>";
}
var_dump($row);
}else{
//查看购物车
$sql = "select count(*) from car where userid = '$inuserid'";
//var_dump($sql );
$rs = $con->query($sql);
$row = $rs->fetch_array();
$num = $row[0];//

//    var_dump($inuserid );
//    var_dump($num );
//判断是否有产品
if($num==0)
{
echo "您未购买任何产品";
}else
{
//输出
$sql = "select car.good_id,car.num,good.id,good.price,good.title,good.content  from car left join good on car.good_id = good.id where car.userid = '$inuserid'";
$rs = $con->query($sql);
while ($row = $rs->fetch_assoc())
{
echo " <tr>

<td>".$row['title']."</td>
<td>".$row['content']."</td>
<td>".$row['price']."</td>
<td><a href ='./cardel.php?id=".$row['good_id']."&del=1'>减一</a></td>
<td>".$row['num']."</td>
<td><a href ='./cardel.php?id=".$row['good_id']."&del=2'>加1</a></td>
<td>".$row['price']*$row['num']."</td>

<td><a href ='./cardel.php?id=".$row['good_id']."&del=del'>删除</a></td>
</tr>";

}
}
//注销goodid

}

//var_dump($inuserid);

?>

</table>
</body>
</html>

猜你喜欢

给我留言

留言无头像?