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

PHP 购物车

2016-08-01 10:37 357 查看
login.php

<style type="text/css">
.biao
{
padding-left:550px;
padding-top:30px;
}
.div
{
padding-left:500px;
padding-top:20px;
}
.but
{
padding-left:200px;
}
</style>
</head>

<body>
<div class="biao">
<h1>登录页面</h1>
</div>
<div class="div">
<form action="loginchuli.php" method="post">
<div>用户名:<input type="text" name="uid"/></div><br />
<div>密码:  <input type="password" name="pwd" /></div><br />
<div class="but">
<input type="submit" value="登录" />
</div>
</form>
</div>


loginchuli.php

<?php
session_start();
include("../dbda.php");
$db=new DBDA();

$uid=$_POST["uid"];
$pwd=$_POST["pwd"];

//根据用户名查密码
$sql="select pwd from users where uid='{$uid}'";
$mima=$db->strquery($sql);//用strquery直接返回密码

$sqlzt="select isok from users where uid='{$uid}'";
$zt=$db->strquery($sqlzt);

//判断用户的输入是否为空
if($uid!=""&&$pwd!="")
{
//判断输入是否正确
if($pwd==$mima)
{
if($zt==1)
{
//把用户名存到session里面
$_SESSION["uid"]=$uid;
header("location:main.php");
}
else
{
echo"还未审核通过";
}
}
else
{
echo"用户名或密码输入错误";
}
}
else
{
echo"用户名或密码不能为空";
}




main.php

<style type="text/css">
.z
{
width:100%;
margin-top:80px;}
.a
{
width:200px;
float:left;
margin-top:35px;}
.a div
{
height:50px;
width:200px;
line-height:50px;
text-align:center;
vertical-align:middle;}
.b
{
width:1000px;
float:left;}
.b1
{
width:980px;
height:35px;
line-height:35px;
vertical-align:middle;
text-align:end;
}
a:link
{
color:#009;
text-decoration:none;}
a:visited
{
color:#009;
text-decoration:none;}
a:hover
{
color:#F30;
text-decoration:none;}
a:active
{
color:#F30;
text-decoration:none;}

</style>
</head>

<body>
<?php
session_start();

if(empty($_SESSION["uid"]))
{
header("location:login.php");
exit;
}
include("../dbda.php");
$db=new DBDA();

$sfruit="select * from fruit";
$afruit=$db->Query($sfruit);

$gouwuche=array();
if(!empty($_SESSION["gwc"]))
{
$gouwuche=$_SESSION["gwc"];
}

//商品数量
$shuliang=count($gouwuche);
//总价
$zongjia=0;
foreach($gouwuche as $v)
{
$sdanjia="select price from fruit where ids='{$v[0]}'";
$danjia=$db->StrQuery($sdanjia);
$zongjia=$zongjia+$danjia*$v[1];
}
?>
<div class="z">
<div class="a">
<div><a href="main.php">浏览商品</a></div>
<div><a href="zhanghu.php">查看账户</a></div>
<div><a href="gouwuche.php">查看购物车</a></div>
</div>
<div class="b">
<div class="b1">
购物车中有:<?php echo $shuliang?>件商品,总价格为:<?php echo $zongjia?>元
</div>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr height="35px" style="font-weight:bold; font-size:16px; text-align:center;">
<td>代号</td>
<td>名称</td>
<td>价格</td>
<td>产地</td>
<td>库存</td>
<td>操作</td>
</tr>
<?php
foreach($afruit as $v)
{
echo"<tr height='25px'; style='text-align:center;'>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td><a href='goumai.php?code={$v[0]}'>购买</a></td>
</tr>";
}
?>
</table>
</div>
</div>


goumai.php

<?php
session_start();

$code=$_GET["code"];

if(empty($_SESSION["gwc"]))
{
//第一次点击购买
$attr = array(
array($code,1)
);

$_SESSION["gwc"] = $attr;
}
else
{
//已经点击过购买
$attr = $_SESSION["gwc"];

//判断该水果是否购买过
$count = 0;//定义一个变量购买的次数
foreach($attr as $v)
{
if($v[0]==$code)
{
$count++;
}
}
//该水果在购物车中存在
if($count>0)
{
foreach($attr as $k=>$v)
{
if($v[0]==$code)
{
$attr[$k][1] = $v[1]+1;
}
}
}
//该水果在购物车中不存在
else
{
$anei = array($code,1);
$attr[] = $anei;
}

//将修改的数组添加到session里面去
$_SESSION["gwc"] = $attr;
}
header("location:main.php");




zhanghu.php

<style type="text/css">
.z
{
width:100%;
margin-top:100px;}
.a
{
width:200px;
float:left;}
.a div
{
height:50px;
width:200px;
line-height:50px;
text-align:center;
vertical-align:middle;}
.b
{
width:1000px;
float:left;}
a:link
{
color:#009;
text-decoration:none;}
a:visited
{
color:#009;
text-decoration:none;}
a:hover
{
color:#F30;
text-decoration:none;}
a:active
{
color:#F30;
text-decoration:none;}

</style>
</head>

<body>
<?php
session_start();

if(empty($_SESSION["uid"]))
{
header("location:login.php");
exit;
}
include("../DBDA.php");
$db = new DBDA();

$uid=$_SESSION["uid"];

$syue="select account from login where username='{$uid}'";
$yue=$db->StrQuery($syue);

?>
<div class="z">
<div class="a">
<div><a href="main.php">浏览商品</a></div>
<div><a href="zhanghu.php">查看账户</a></div>
<div><a href="gouwuche.php">查看购物车</a></div>
</div>
<div class="b">
您的账户余额为:<?php echo $yue?>元
</div>
</div>




gouwuche.php

<style type="text/css">
.z
{
width:100%;
margin-top:100px;}
.a
{
width:200px;
float:left;}
.a div
{
height:50px;
width:200px;
line-height:50px;
text-align:center;
vertical-align:middle;}
.b
{
width:1000px;
float:left;}
.c
{
width:200px;
height:50px;
line-height:50px;
vertical-align:middle;
text-align:center;
margin-left:800px;}
a:link
{
color:#009;
text-decoration:none;}
a:visited
{
color:#009;
text-decoration:none;}
a:hover
{
color:#F30;
text-decoration:none;}
a:active
{
color:#F30;
text-decoration:none;}

</style>
<script src="../jquery-1.11.2.min.js"></script>
</head>

<body>
<?php
session_start();

if(empty($_SESSION["uid"]))
{
header("location:login.php");
exit;
}
include("../DBDA.php");
$db = new DBDA();

$attr = array();
if(!empty($_SESSION["gwc"]))
{
$attr = $_SESSION["gwc"];
}
?>
<div class="z">
<div class="a">
<div><a href="main.php">浏览商品</a></div>
<div><a href="zhanghu.php">查看账户</a></div>
<div><a href="gouwuche.php">查看购物车</a></div>
</div>
<div class="b">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr height="35px" style="font-weight:bold; font-size:16px; text-align:center;">
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>操作</td>
</tr>
<?php
foreach($attr as $v)
{
$smcdj = "select * from fruit where Ids='{$v[0]}'";
$sginfo = $db->Query($smcdj);

echo"<tr height='25px'; style='text-align:center;'>
<td>{$sginfo[0][1]}</td>
<td>{$sginfo[0][2]}</td>
<td>{$v[1]}</td>
<td><a href='shanchu.php?code={$v[0]}'>删除</a></td>
</tr>";
}
?>
</table>
<div class="c">
<a id="tj" href="tijiao.php">提交订单</a> <span id="xinxi"></span>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(e){
$("#tj").click(function(){

var bs=false;

$.ajax({
async:false,
url:"check.php",
dataType:"TEXT",
success: function(data){
if(data.trim()=="OK")
{
bs=true;
}
else
{
$("#xinxi").html(data);
$("#xinxi").css("color","red");
}
}
});
return bs;
})
});
</script>


shanchu.php

<?php
session_start();

$attr = $_SESSION["gwc"];
$code = $_GET["code"];

foreach($attr as $k=>$v)
{
if($v[0]==$code)
{
//判断数量是否大于1
if($v[1]>1)
{
$attr[$k][1] = $attr[$k][1]-1;
}
else
{
unset($attr[$k]);
}
}
}

$_SESSION["gwc"] = $attr;

header("location:gouwuche.php");




check.php

<?php
session_start();
include("../dbda.php");
$db=new DBDA();

//取出购物车信息
$gwc=$_SESSION["gwc"];
//去除登陆者的用户名
$uid=$_SESSION["uid"];
//总价
$zongjia=0;
foreach($gwc as $v)
{
$sdanjia="select price from fruit where ids='{$v[0]}'";
$danjia=$db->StrQuery($sdanjia);
$zongjia=$zongjia+$danjia*$v[1];
}
//余额
$syue="select account from login where username='{$uid}'";
$yue=$db->StrQuery($syue);

if($yue>=$zongjia)
{
//判断库存
$dh="";

foreach($gwc as $v)
{
//根据水果代号:$v[0]取到库存数量
$skucun="select numbers from fruit where ids='{$v[0]}'";
$kuncun=$db->StrQuery($skucun);

if($kuncun<$v[1])
{
$dh=$v[0];
break;
}
}
if($dh=="")
{
echo"OK";
}
else
{
$sname="select name from fruit where ids='{$dh}'";
$name=$db->StrQuery($sname);

echo "{$name}库存不足";
}
}
else
{
echo"余额不足";
}


tijiao.php

<?php
session_start();

include("../dbda.php");
$db=new DBDA();

//取购物车信息
$gwc=$_SESSION["gwc"];
//取登陆者的用户名
$uid=$_SESSION["uid"];

//1.减余额
//总价
$zongjia=0;
foreach($gwc as $v)
{
$sdanjia="select price from fruit where ids='{$v[0]}'";
$danjia=$db->StrQuery($sdanjia);
$zongjia=$zongjia+$danjia*$v[1];
}

$sjian="update login set account=account-{$zongjia} where username='{$uid}'";
$db->Query($sjian,0);
//2.减库存
foreach($gwc as $v)
{
$skjian="update fruit set numbers=numbers-{$v[1]} where ids='{$v[0]}'";
$db->Query($skjian,0);
}
//3.添加订单信息
$ordercode=date("YmdHis");
$time=date("Y-m-d H:i:s");
$sdingdan="insert into orders values('{$ordercode}','{$uid}','{$time}')";
$db->Query($sdingdan,0);

//添加订单详情
foreach($gwc as $v)
{
$sxiangqing="insert into orderdetails values('','{$ordercode}','{$v[0]}','{$v[1]}') ";
$db->Query($sxiangqing,0);
}

header("location:main.php");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: