• 购物车的例子


    <?php
    session_start();
    include("fengzhuanglei.php");
    $db = new DBDA();
    ?>
    
    <!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">
    .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    </style>
    </head>
    
    <body>
    
    
    
    <div style="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="100%; height:600px">
    	<div id="left" style="20%; float:left">
        	<a href="gouwuche.php"><div class="list">浏览商品</div></a>
            <a href="zhanghu.php"><div class="list">查看账户</div></a>
            <a href="gouwu.php"><div class="list">查看购物车</div></a>
        </div>
        
    	<div id="right" style="80%; float:left">
        
        <?php
    	   $gwc = array();
    	   if(!empty($_SESSION["gwc"]))
    	   {
    		  $gwc = $_SESSION["gwc"]; 
    	   }
    	   
    	   $zhonglei = count($gwc);
    	   $sum = 0;
    	   foreach($gwc 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>";
    	
    	?>
    	   
    
        
        
        	<table width="100%" border="1" cellpadding="0" cellspacing="0">
            <tr>
            	<td>代号</td>
                <td>水果名称</td>
                <td>水果价格</td>
                <td>源产地</td>
                <td>库存量</td>
                <td>操作</td>
            </tr>
            
            <?php
    	
    		
    		$sql = "select * from fruit";
    		$attr = $db->Query($sql);
    		
    		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='goumai.php?code={$v[0]}'>购买</a></td></tr>";
    		}
    		?>
            
            </table>
        </div>
    </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" />
    <script src="jquery-1.11.2.min.js"></script>
    <title>无标题文档</title>
    <style type="text/css">
    .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    </style>
    </head>
    
    <body>
    
    
    
    <div style="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="100%; height:600px">
    	<div id="left" style="20%; float:left">
        	<a href="gouwuche.php"><div class="list">浏览商品</div></a>
            <a href="zhanghu.php"><div class="list">查看账户</div></a>
            <a href="gouwu.php"><div class="list">查看购物车</div></a>
        </div>
        
    	<div id="right" style="80%; float:left">
        
        
        
        	<table width="100%" border="1" cellpadding="0" cellspacing="0">
            <tr>
            	<td>商品名称</td>
                <td>商品价格</td>
                <td>购买数量</td>
                <td>操作</td>
            </tr>
            
            <?php
    			include("fengzhuanglei.php");
    			$db = new DBDA();
    			$attr = array();
    			if(!empty($_SESSION["gwc"]))
    			{ 
    			    $attr = $_SESSION["gwc"];
    			}
    			foreach($attr as $k=>$v)
    			{ 
    				$sql = "select name,price from fruit where ids='{$v[0]}'";
    				$shui = $db->Query($sql);
    				echo"<tr><td>{$shui[0][0]}</td><td>{$shui[0][1]}</td><td>{$v[1]}</td><td><a href='shanchu.php?sy={$k}'>删除</a></td></tr>";
    			}
    			
    		?>
            
            </table>
           <div ><input type="button" id="tj" value="提交订单" /></div><div id="ts"></div>
        </div>
    </div>
    
    </body>
    <script type="text/javascript">
    
       $("#tj").click(function(){
    	   
    	   
    	   })
    </script>
    </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)
    	{
    		if($v[0]==$code)
    		{
    			$bs=1;
    			$attr[$k][1] = $attr[$k][1]+1;
    		}
    	}
    	//如果没有在数组里面出现
    	if($bs==0)
    	{
    		$shuzu = array($code,1);
    		$attr[] = $shuzu;
    	}
    	
    	$_SESSION["gwc"]=$attr;
    	
    }
    header("location:gouwuche.php");
    

    删除页面:

    <?php
    session_start();
    
    $sy = $_GET["sy"];
    $attr = $_SESSION["gwc"];
    
    if($attr[$sy][1]>1)
    {
    	$attr[$sy][1] = $attr[$sy][1]-1;
    }
    else
    {
    	unset($attr[$sy]);
    	$attr = array_values($attr);
    }
    
    $_SESSION["gwc"]=$attr;
    
    header("location:gouwu.php");
    

      提交页面:

    <?php
    session_start();
    include("fengzhuanglei.php");
    $db = new DBDA();
    $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($attr 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 "YEBUZU";
    		exit;
    	}
    	
    //判断库存是否满足
    
    foreach($attr as $v)
    {
    	$skucun = "select name,numbers from fruit where ids='{$v[0]}'";
    	$akucun = $db->Query($skucun);
    	if($akucun[0][1]<$v[1])
    	{
    		$bs = false;
    		echo "{$akucun[0][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="update login set account=account-{$sum} where username='{$uid}'";
    	$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";
    

      

  • 相关阅读:
    每日总结19
    每日博客
    每日博客
    每日博客
    每日博客
    今日收获
    python 基础学习
    python 基础学习
    python 基本语法学习
    【Rust】格式化Formatting
  • 原文地址:https://www.cnblogs.com/gaojunshan/p/6065306.html
Copyright © 2020-2023  润新知