码迷,mamicode.com
首页 > Web开发 > 详细

php 简易购物习题

时间:2016-06-29 20:39:15      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

1.货物界面

技术分享
<!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">
#main{ width:100%}
#left{ width:20%; float:left}
#right{ width:80%; float:left}
</style>
</head>

<body>
<?php
session_start();

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

$sql="select * from fruit";

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

$gouwuche=array();
if(!empty($_SESSION["gwc"]))
{
    $gouwuche=$_SESSION["gwc"];    
}
//商品数量
$shuliang=count($gouwuche);
//总价
$zongjia=0;

foreach($gouwuche as $v)
{
    $sprice="select price from fruit where ids=‘{$v[0]}‘";
    $price=$db->StrQuery($sprice);    
    $zongjia=$zongjia+$price*$v[1];
}

?>
<div id="main">
    <div id="left">
        <div><a href="gouwu.php">浏览商品</a></div>
        <div><a href="gouwu4.php">查看账户</a></div>
        <div><a href="gouwu3.php">查看购物车</a></div>
    </div>
    <div id="right">
        <div>
            购物车中有:<?php echo $shuliang ?>件商品,总价格为:<?php echo $zongjia ?></div>
        <table width="100%" border="1" cellpadding="0" cellspacing="0">
            <tr>
                <td>代号</td>
                <td>名称</td>
                <td>价格</td>
                <td>产地</td>
                <td>库存</td>
                <td>购买</td>
            </tr>
            <?php
            foreach($attr as $v)
            {
                echo"<tr>
                <td>{$v[0]}</td>
                <td>{$v[1]}</td>
                <td>{$v[2]}</td>
                <td>{$v[3]}</td>
                <td>{$v[4]}</td>
                <td><a href=‘gouwuchuli.php?code={$v[0]}‘>购买</a></td>
                </tr>";    
            }
            ?>
        </table>
    </div>
</div>

</body>
</html>
View Code
技术分享
<?php
session_start();

//$_SESSION["gwc"];

$code = $_GET["code"];

/*$attr=array(
    array("coo1",5)
);*/

//第一次点击购买
if(empty($_SESSION["gwc"]))
{
    /*$attr=array();//造二维数组外层的数组
    $anei=array();//造二维数组里层的数组
    $anei[]=$code;
    $anei[]=1;    
    
    $attr[]=$anei;*/
    
    $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:gouwu.php");
View Code

2.登陆界面

技术分享
<!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>
<h1>登陆</h1>
<form action="gouwu2chuli.php" method="post">
<div>用户名:<input type="text" name="uid" /></div>
<div>密码:<input type="password" name="pwd" /></div>
<input type="submit" value="登陆" />
</form>
</body>
</html>
View Code
技术分享
<?php
session_start();
include("DBDA.php");
$db=new DBDA();

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

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

$mima=$db->StrQuery($sql);

if($pwd==$mima && $pwd!="")
{
    $_SESSION["uid"]=$uid;
    header("location:gouwu.php");
}
else
{
    header("location:gouwu2.php");    
}
View Code

3.购物车

技术分享
<!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">
#main{ width:100%}
#left{ width:20%; float:left}
#right{ width:80%; float:left}
</style>
<script src="jquery-1.11.2.min.js"></script>
</head>

<body>
<?php
session_start();

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

$attr=array();
if(!empty($_SESSION["gwc"]))
{
    $attr=$_SESSION["gwc"];    
}
?>
<div id="main">
    <div id="left">
        <div><a href="gouwu.php">浏览商品</a></div>
        <div><a href="gouwu4.php">查看账户</a></div>
        <div><a href="gouwu3.php">查看购物车</a></div>
    </div>
    <div id="right">
        <table width="100%" border="1" cellpadding="0" cellspacing="0">
            <tr>
                <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>
                <td>{$sginfo[0][1]}</td>
                <td>{$sginfo[0][2]}</td>
                <td>{$v[1]}</td>
                <td><a href=‘gouwu3chuli.php?code={$v[0]}‘>删除</a></td>
                </tr>";    
            }
            ?>
        </table>
        <div>
            <a id="tj" href="gouwu3chuli2.php">提交订单</a> <span id="xinxi"></span>
        </div>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#tj").click(function(){
        var bs=false;
        $.ajax({
            async:false,
            url:"gouwu3chuli3.php",
            dataType:"TEXT",
            success: function(data)
            {
                if(data.trim()=="OK")
                {
                    bs="true";
                }
                else
                {
                    $("#xinxi").html(data);    
                    $("#xinxi").css("color","#F00");    
                }
                
            }
            
            });
            
        return bs;
        
        })
});
</script>
</body>
</html>
View Code
技术分享
<?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:gouwu3.php");
View Code
技术分享
<?php
session_start();

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

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

//总价
$zongjia = 0;
foreach($attr as $v)
{
    $sprice = "select price from fruit where ids=‘{$v[0]}‘";
    $price = $db->StrQuery($sprice);
    $zongjia = $zongjia + $price*$v[1];
}

//1.减余额
$sjian = "update login set account = account-{$zongjia} where username=‘{$uid}‘";
$db->Query($sjian,0);

//2.减库存
foreach($attr 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($attr as $v)
{
    $sxiangqing = "insert into orderdetails values(‘‘,‘{$ordercode}‘,‘{$v[0]}‘,‘{$v[1]}‘)";
    $db->Query($sxiangqing,0);
}

header("location:gouwu.php");
View Code
技术分享
<?php
session_start();

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

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

//总价
$zongjia = 0;
foreach($attr as $v)
{
    $sprice = "select price from fruit where ids=‘{$v[0]}‘";
    $price = $db->StrQuery($sprice);
    $zongjia = $zongjia + $price*$v[1];
}

//余额
$sql = "select account from login where username=‘{$uid}‘";
$yue = $db->StrQuery($sql);

if($yue>=$zongjia)
{
    //判断库存
    $daihao = "";
    
    foreach($attr as $v)
    {
        //取到库存
        $skucun = "select numbers from fruit where ids=‘{$v[0]}‘";
        $kucun = $db->StrQuery($skucun);
        //如果库存小于数量
        if($kucun<$v[1])
        {
            $daihao = $v[0];
            break;
        }
    }
    
    if($daihao =="")
    {
        echo "OK";
    }
    else
    {
        $sname = "select name from fruit where ids=‘{$daihao}‘";
        $name = $db->StrQuery($sname);
        
        echo "{$name}库存不足!";
    }
}
else
{
    echo "余额不足!";
}
View Code

4.余额界面

技术分享
<!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">
#main{ width:100%}
#left{ width:20%; float:left}
#right{ width:80%; float:left}
</style>
</head>

<body>
<?php
session_start();

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

$uid=$_SESSION["uid"];

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

?>
<div id="main">
    <div id="left">
        <div><a href="gouwu.php">浏览商品</a></div>
        <div><a href="gouwu4.php">查看账户</a></div>
        <div><a href="gouwu3.php">查看购物车</a></div>
    </div>
    <div id="right">
        您的账户余额为:<?php echo $yue ?></div>
</div>

</body>
</html>
View Code

 

php 简易购物习题

标签:

原文地址:http://www.cnblogs.com/bilibiliganbei/p/5628051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!