php 简易购物习题
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>
<?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");
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>
<?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");
}
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>
<?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");
<?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");
<?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 "余额不足!";
}
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>
php 简易购物习题的更多相关文章
- springMVC+angular+bootstrap+mysql的简易购物网站搭建
springMVC+angular+bootstrap+mysql的简易购物网站搭建 介绍 前端的css框架用了bootstrap, 以及bootstrap的JS组件, 以及很好用的angular(a ...
- Python3 函数实践之简易购物系统
函数实践之简易购物系统 项目主要需求: 用户可以自行选择功能 该购物系统具有注册/登录/购物/购物车/退出登录功能 用户在登录后才能使用购物/购物车/退出登录功能 ''' 注册 登录 购物 购物车 退 ...
- Php实现简易购物商城系统
实现功能: 1.系统功能模块包括: 1)登陆注册模块 包括验证码.找回密码.注册模块中要使用Ajax判断用户名是否已经存在,使用正则表达式判断电子邮件.手机号和用户密码的格式是否合法. 2)用户管理模 ...
- 基于springMVC+angular+bootstrap+mysql的简易购物网站搭建
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=10&cad=rja& ...
- what's the python之if判断、while循环以及for循环
Python缩进原则 顶级代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进 同一级别的代码,缩进必须一致 官方建议缩进用4个空格 Python程序语言指定任何非0和非空的布 ...
- Python3 函数基础2
目录 可变长参数 可变长形参: *args 可变长实参: *容器类 可变长形参: **kwargs 可变长实参: **字典 函数对象 引用 当做容器类型元素 当做参数传给一个函数 当做函数的返回值 函 ...
- PHP网上商城
页面展示: 代码参考:Php实现简易购物商城系统 - 邵文 - 博客园 (cnblogs.com)
- Android学习之路——简易版微信为例(一)
这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...
- C#【结对编程作业】小学数学习题助手
一.软件成品展示 软件本体下载(包括程序及其更新日志,源码工程包,UML图,API接口文档,算法介绍文档,算式计算excel实例,浅查重程序) 链接: http://pan.baidu.com/s/1 ...
随机推荐
- iOS开发——多线程篇——RunLoop
一.简介 1.什么是RunLoop从字面意思看运行循环跑圈 基本作用保持程序的持续运行处理App中的各种事件(比如触摸事件.定时器事件.Selector事件)节省CPU资源,提高程序性能:该做事时做事 ...
- c# random string
var randomString= Path.GetRandomFileName(); 返回 ar1opgzw.1gp 详细 http://www.dotnetperls.com/random-str ...
- VB .NET周期实现
这里仅提供一个方案,当然会有比本方案更好的,欢迎提供. 简介 在vb.net实现周期调度的问题时(就是间隔固定的时间做什么事情),我们最先想到的一定是(反正我是)利用timer(定时器)来做这个计时的 ...
- Sound Generator 原理
Sound Generator 原理 旨在简单的阐述声音如何通过单片机模块来产生. 声音 声音的种类有千千万,但归根到底还是属于波.在对声音进行模拟转数字的时候,采样越高, 声音被还原的越逼真. 声音 ...
- 百度网盘爬虫Python
": for i in range((shares["total_count"]-1)/ONESHAREPAGE): try: dbcurr.execute('INSER ...
- 2016年11月5日--marquee标签、插入百度地图
<marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee标记不仅可以移动文字,也可以移动图片,表格等. 语法:<marquee> ...
- BZOJ 3270: 博物馆
Sol 期望DP+高斯消元. 根据本题题意列出期望方程\[E(i,j)=(1-p_i)(1-p_j)E(u,v)+(1-p_i)p_jE(u,j)+p_i(1-p_j)E(i,v)+p_ip_jE(i ...
- linux回收站设计
linux回收站设计 在windows下有一个很好的东西,那就是回收站,虽然有很多人批评它.linux不是没有回收站,很多桌面环境都可以看到是有回收站的. 这里是讨论如何设计一个回收站,而不是有没有的 ...
- 【Networking】flannel,pipework,weave,udp,vxlan,ovs等资料
Add Open vSwitch-based multitenant backend for use with OpenShift / Kubernetes: https://github.com/ ...
- 【微服务】SpringBoot、SpringCloud相关
深入学习微框架:Spring Boot: http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ Spring Boot--2 ...