例子:购物车的简单实现

购物车例子:

登录界面:验证用户名与密码是否匹配

<!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" />
<script src="../jquery-1.11.2.min.js"></script>
<title>无标题文档</title>
</head>

<body>

<div>账号:<input type="text" id="uid" /></div>
<div>密码:<input type="text" id="pwd" /></div>
<input type="button" value="登录" id="btn" />

</body>
</html>
<script type="text/javascript">
$("#btn").click(function(){
    var u = $("#uid").val();
    var p = $("#pwd").val();

    $.ajax({
        url:"zhuyecl.php",
        data:{u:u,p:p},
        type:"POST",
        dataType:"TEXT",
        success: function(data){
            
            if(data.trim()=="OK")
            {
                window.location.href="denglu.php";
            }
            else
            {
                alert("账号或密码错误");
            }
            
            }
        
        
        })

    })

</script>

php处理登录:

<?php
session_start();
$uid = $_POST["u"]; 
$pwd = $_POST["p"];

include("../fengzhuang.class.php");

$db = new fengzhuang();

$sql = "select password from login where username='{$uid}'";

$mm = $db->strquery($sql);

if($mm = $pwd && $pwd!="")
{
    $_SESSION["uid"]=$uid;    //把用户名存入session
    echo "OK";
}
else
{
    echo "NO";
}

商品显示主页:

<!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>
<style type="text/css">
.dh{ width:100px; height:60px; color:#C06}
.dh:hover{ color:#FFF; background-color:#069; border-radius:7px; cursor:pointer}
</style>
</head>

<body>

<!--背景色-->
<div style=" width:1347px; height:600px; background-color:#09C"></div>

<!--标题-->
<div style="font-size:35px; color:#FFF; position:relative; margin-top:-550px; margin-left:550px; font-weight:bold">大苹果购物网</div>

<!--显示出来的水果表信息-->
<div style="width:700px; position:relative; top:100px; left:400px; text-align:center; line-height:30px; vertical-align:middle; font-weight:bold">
<table width="700" cellpadding="0" cellspacing="0" style="background-color:#069; border-radius:8px">
    <tr>
        <td>代号</td>
        <td>水果名称</td>
        <td>水果价格</td>
        <td>原产地</td>
        <td>货架</td>
        <td>库存量</td>
        <td>操作</td>
    </tr>
       
       
       <?php

include("../fengzhuang.class.php");
$db = new fengzhuang();

$sql = "select ids,name,price,source,shelves,numbers from fruit";

$attr = $db->query($sql);

foreach($attr as $v)
{
    echo "<tr style='background-color:white'>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$v[2]}</td>
        <td>{$v[3]}</td>
        <td>{$v[4]}</td>
        <td>{$v[5]}</td>
        <td><a href='goumai.php?code={$v[0]}'>购买</a></td>
        </tr>";
}

?>
</table>
</div>

<!--导航-->
<div style=" width:100px; position:relative; margin-top:-100px; margin-left:270px; line-height:60px; text-align:center; 
vertical-align:middle; background-color:#FFF; border-radius:7px"> <a href="main.php"><div class="dh">浏览商品</div></a> <a href="zhanghu.php"><div class="dh">查看账户</div></a> <a href="gouwuche.php"><div class="dh">查看购物车</div></a> </div> <!--技术链接--> <div style=" width:200px; color:#FFF; position:relative; font-size:20px; position:relative; top:120px; left:580px">天启网络版权所有</div> </body> </html>

点击购买处理页面:

<?php
session_start();
$code = $_GET["code"];

//因为购买的数量增加,不能只存一个主键值,还要存数量,用二维数组存
if(empty($_SESSION["gwc"]))
{
    //第一次点击购买
    $attr = array(array($code,1));
    $_SESSION["gwc"] = $attr;
}
else
{
    //不是第一次点击购买
    $attr = $_SESSION["gwc"];
    
    $bs = 0;
    foreach($attr as $k=>$v)        //复制一份给予V 可以进行判断、读取,但不可以进行操作
    {
        if($v[0]==$code)
        {
            $bs=1;
            $attr[$k][1] = $attr[$k][1]+1;    //水果代号出现加1
        }
    }
    //如果没有在数组里面出现
    if($bs==0)
    {
        $shuzu = array($code,1);
        $attr[] = $shuzu;
    }
    $_SESSION["gwc"]=$attr;
}

点击查看购物车,跳转界面,实现当点击购买时数量增加,点击删除时数量减少

<?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>
<style type="text/css">
.dh{ width:100px; height:60px; color:#C06}
.dh:hover{ color:#FFF; background-color:#069; border-radius:7px; cursor:pointer}
</style>
</head>

<body>

<!--背景色-->
<div style=" width:1347px; height:600px; background-color:#09C"></div>

<!--标题-->
<div style="font-size:35px; color:#FFF; position:relative; margin-top:-550px; margin-left:550px; font-weight:bold">大苹果购物网</div>

<!--显示出来的水果表信息-->
<div style="width:700px; position:relative; top:100px; left:400px; text-align:center; line-height:30px; vertical-align:middle; font-weight:bold">
<table width="700" cellpadding="0" cellspacing="0" style="background-color:#069; border-radius:8px">
    <tr>
        <td>商品名称</td>
        <td>商品单价</td>
        <td>购买数量</td>
        <td>操作</td>
    </tr>
       
       
<?php
include("../fengzhuang.class.php");
$db = new fengzhuang();

$attr = array();
if(!empty($_SESSION["gwc"]))
{
    $attr = $_SESSION["gwc"];
}
foreach($attr as $v)
{
    $sql = "select name,price from fruit where ids='{$v[0]}'";
    $ashuiguo = $db->query($sql);
    
    echo "<tr><td>{$ashuiguo[0][0]}</td><td>{$ashuiguo[0][1]}</td><td>{$v[1]}</td><td>删除</td></tr>";
}

?>
</table>
</div>

<!--导航-->
<div style=" width:100px; position:relative; margin-top:-100px; margin-left:270px; line-height:60px;
text-align:center; vertical-align:middle; background-color:#FFF; border-radius:7px"
> <a href="main.php"><div class="dh">浏览商品</div></a> <a href="zhanghu.php"><div class="dh">查看账户</div></a> <a href="gouwuche.php"><div class="dh">查看购物车</div></a> </div> <!--技术链接--> <div style=" width:200px; color:#FFF; position:relative; font-size:20px; position:relative; top:120px; left:580px">天启网络版权所有</div> </body> </html>

建删除界面(纯php操作):

<?php
session_start();
$sy = $_GET["sy"];

$attr = $_SESSION["gwc"];

if($attr[$sy][1]>1)
{
    //如果数量大于1,减1
    $attr[$sy][1] = $attr[$sy][1]-1;
}
else
{
    //如果数量小雨1,直接移除
    unset($attr[$sy]);
    $attr = array_values($attr);
}
$_SESSION["gwc"]=$attr;

header("location:gouwuche.php");

动态显示购买的商品信息(在主页内嵌入php代码):

//首先开启session
<?php
session_start();

include("../fengzhuang.class.php");
$db = new fengzhuang();
?>

//在表格上面加php代码,用div显示一句话 <?php $agwc = array(); if(!empty($_SESSION["gwc"])) { $agwc = $_SESSION["gwc"]; } $zhonglei = count($agwc); //种类数量 $sum = 0; //总价格之和 foreach($agwc as $v) { $sql = "select price from fruit where ids='{$v[0]}'"; $danjia = $db->strquery($sql); $sum = $sum+$danjia*$v[1];//单加乘以数量 } echo "<div>购物车中有:{$zhonglei}种商品,总价格为:{$sum}元.</div>"; ?>

订单操作、在购物车界面加提示

<div id="tj">提交订单</div><div id="ts"></div>        //在这里加在表格下了

在购物车界面嵌入ajax代码:
<script type="text/javascript">
$("#tj").click(function() {
    $.ajax({
        url:"dingdan.php",
        dataType:"TWXT",
        success: function(){
            
            if(data.trim()=="OK")
            {
                alert("购买成功");
            }
            else if(data.trim()=="YEBZ")
            {
                $("#ts").html("余额不足")
                $("#ts").css("color","red")
            }
            else
            {
                $("#ts").html(data)
                $("#ts").css("color","red")
            }
        }
        
    })
});


</script>

订单处理界面:

<?php
session_start();
include("../fengzhuang.class.php");
$db = new fengzhuang();

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

//看下两个条件是否都满足
$bs = true;



//判断余额是否满足

    //根据用户名找余额
    $syue = "select account from login where username='{$uid}'";
    $yue = $db->strquery($syue);
    
    //根据购物车数组取总金额
    $sum = 0;  //总价格之和
foreach($agwc as $v)
{
    $sql = "select price from fruit where ids='{$v[0]}'";
    $danjia = $db->strquery($sql);
    
    $sum = $sum+$danjia*$v[1];//单加乘以数量
}
if($yue<$sum)
{
    $bs=false;
    echo "YEBZ";
    exit;
}




//判断库存是否满足

foreach($attr as $v)
{
    $skucun = "select name,numbers from fruit where ids='{$v[0]}'";
    $akucun = $db->query($skucun);
    if($akucun[1]<$v[1])
    {
        $bs=false;
        echo "{$akucun[0]}库存不足!";
        exit;
    }
}




//添加订单,减库存,减余额
if($bs)
{
    //减库存
    foreach($attr as $v)
    {
        $sql = "update fruit set numbers = numbers-{$v[1]} where ids='{$v[0]}'";
        $db->query($sql,0);
    }
    
    //减余额
    $jianyue="undate login set account=account-{$sum} where ids='{$v[0]}'";
    $db->query($jianyue,0);
    
    //添加订单
    $dingdanhao = $uid+date("YmdHis");
    $t = time();
    
    $sorder = "insert into orders values('{$dingdanhao}','{$uid}','{$t}')";
    
    $db->query($sorder,0);
    //订单详情表
    foreach($attr as $v)
    {
        $sxq = "insert into orderdetails values('','{$dingdanhao}','{$v[0]}','$v[1]')";
        $db->query($sxq,0);
    }
}
echo "OK";

 

查看账户:

<?php
session_start();
$uid = $_SESSION['uid'];
?>
<!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><br />
<style type="text/css">
.list{ width:100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
</style>
</head>
<body>
<div style="width:100%; height:100px; background-color:#6CC">
    <h1 style="float:left">大苹果商城</h1>
    <a style="float:right; margin-top:40px" href="zhuxiao.php">注销</a>
</div>
<br />
<div style="width:100%; height:600px">
    <div id="left" style="width:20%; float:left">
        <a href="main.php"><div class="list">浏览商品</div></a>
        <a href="zhanghu.php"><div class="list">查看账户</div></a>
        <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    </div>    
    <div id="right" style="width:80%; height:150px; float:left">
<?php
        include("../DBDA.class.php");
        $db = new DBDA();
        $sql = "select Account from login where UserName='{$uid}'";
        $result = $db->strQuery($sql);
        
        echo ("您的账户中还剩余".$result);
?>
    </div>
</div>

</body>
</html>

 

posted @ 2016-11-15 14:33  厚爱无需我多言。  阅读(654)  评论(0编辑  收藏  举报