简单购物车的实现,session的使用
购物车浏览商品界面代码
<!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>
<link rel="stylesheet" href="888.css" type="text/css" />
<script>
function test()
{
if(!confirm('确认购买吗?'))
return false;
}
</script>
</head> <body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><a href="888_ok.php">浏览图书</a></li>
<li><a href="shop888.php">查看购物车</a></li>
<li><a href="clearshop.php">清空购物车</a></li>
</ul>
</div>
<div id="show">
<table width="90%" align="center" border="" cellpadding="" cellspacing="">
<tr>
<td width="5%" valign="middle">
<td width="5%" align="center">id</td>
<td width="20%" align="center">书名</td>
<td width="10%" align="center">价格</td>
<td width="30%" align="center">出版日期</td>
<td width="10%" align="center">类型</td>
</tr> <?php
include_once("../conn/conn.php");
$sqlstr="select * from tb_demo01 order by id";
$result=mysql_query($sqlstr,$conn);
while($rows=mysql_fetch_array($result))
{
echo"<tr><td align='left' height='25'> ";
echo "<input type=checkbox name='chk[]' id='chk' value=".$rows[].">";
echo"</td>";
for($i=;$i<count($rows);$i++)
{
echo"<td align='center' height='25'>".$rows[$i]."</td>";
}
echo"<td><a href='addshop11.php?id={$rows[0]}' onclick='return test()'>加入购物车</a></td>"; } ?>
</div>
</body>
</html>

点击加入购物车按钮后进入此页面,获取id进行传值
<?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>
<link rel="stylesheet" href="shop888.css" type="text/css" />
</head>
<body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><h3>添加商品到购物车,返回到<a href="shop888.php">我的购物车</a></h3></li>
</ul>
</div>
<div id="show">
<?php
include_once("../conn/conn.php");
$sql="select * from tb_demo01 where id={$_GET['id']}";
$result=mysql_query($sql,$conn);
if(empty($result)||mysql_num_rows($result)==)
{
die("没有找到要购买的信息");
}
else
{
$shop=mysql_fetch_array($result);
}
$shop["num"]=; //添加一个数量的字段
//var_dump($shop);
//如果存在则实现数量的累加,如果不存在则不累加
if(isset($_SESSION["shoplist"][$shop['id']]))
{
//如果存在,数量++
$_SESSION["shoplist"][$shop['id']]['num']++;
}
else
//如果不存在
{
$_SESSION["shoplist"][$shop['id']]=$shop;
}
?>
</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" />
<title>无标题文档</title>
<link rel="stylesheet" href="shop888.css" type="text/css" />
<html>
<head></head>
<body>
<div id="banner"></div>
<div id="link">
<ul type="none">
<li><h3>我的购物车,返回商城<a href="888_ok.php">继续购买</a></h3></li>
</ul>
</div>
<div id="show">
<table border="" width=% cellpadding="" cellspacing="">
<tr>
<th>id</th>
<th>书名</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>日期</th>
<th>类型</th>
</tr>
<?php
$sum=;
if(isset($_SESSION["shoplist"]))
{
foreach($_SESSION["shoplist"] as $value)
{
echo"<tr>";
echo"<td>{$value[0]}</td>";
echo"<td>{$value[1]}</td>";
echo"<td>{$value[2]}</td>";
echo"<td>
<button onclick='window.location.href='updateshop.php?id=$value[]&num=-''>-</button>
{$value['num']}
<button onclick='window.location.href='updateshop.php?id=$value[]&num=''>+</button>
</td>";//数量
echo"<td>".$value[]*$value['num']."</td>";
echo"<td>{$value[3]}</td>";
echo"<td>{$value[4]}</td>";
echo"<td><a href='clearshop.php?id=$value[0]'>删除</a></td>";
echo"</tr>";
$sum+=$value[]*$value['num'];
}
}
?>
<tr>
<th>总计金额:</th>
<th colspan="" align="right"><?php echo $sum; ?></th>
<td></td>
</tr>
</table>
</div>
</body>
</html>

购物车清空页面
<?php
session_start();//启动会话
//清空session中的商品
if($_GET['id'])
{
unset($_SESSION["shoplist"][$_GET['id']]);
}
else
{
unset($_SESSION["shoplist"]);
}
//跳转到购物车界面
header("Location:shop888.php"); ?>
<!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>
</body>
</html>

简单购物车的实现,session的使用的更多相关文章
- Session小案例-----简单购物车的使用
Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...
- java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)
1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...
- 一个简单的C#获取Session、设置Session类文件
一个简单的C#获取Session.设置Session类文件,本类主要实现大家最常用的两个功能: 1.GetSession(string name)根据session名获取session对象: 2.Se ...
- 简单购物车程序(Python)
#简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...
- python实现简单购物车系统(练习)
#!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('comput ...
- Python实例---简单购物车Demo
简单购物车Demo # version: python3.2.5 # author: 'FTL1012' # time: 2017/12/7 09:16 product_list = ( ['Java ...
- 用Python实现简单购物车
作业二:简单购物车# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,# 如果输入为空或其他非法输入则要求用户重新输入 shopping_list = [] w ...
- Session 简单购物车
package session.test; import java.io.IOException; import java.io.PrintWriter; import java.util.Linke ...
- 2017.9.28 web设计简单的购物车应用案例--session的简单应用
该购物过程是在session范围内完成的,需要使用session对象实现信息的共享 (1)购买“肉类”商品的页面 <%@ page language="java" impor ...
随机推荐
- Google浏览器提示用户要允许网站允许flash!
开发的flash播放器,在谷歌浏览器上播放不了,需要提示用户手动开启允许网站运行flash: <script type="text/javascript"> if (w ...
- linux对文件某列求和
对文件某列求和: -F,用,号分隔,求第3行的和 awk -F, '{sum += $3};END {print sum}' test
- vuls漏洞扫描工具
Vuls 是一款适用于 Linux/FreeBSD 的漏洞扫描程序,无代理,采用 Go 语言编写,对于系统管理员来说,每天必须执行安全漏洞分析和软件更新都是一个负担. 为避免生产环境宕机,系统管理员通 ...
- 网站微图标,页标签,favicon.ico
随便打开一个网页:比如 http://www.baidu.com/ 可以看到在浏览器的标签头上面显示了一个图标,也就是我们常说的favicon.ico, 由于这篇文章主要讨论favicon.ico,以 ...
- HDU 6016 Count the Sheep
Count the Sheep Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- rac库grid目录权限(6751)导致数据库宕机案例 此方法仅用于紧急救助
问题: 我的rac环境不小心通过chown命令改变了/u01目录及其子目录的权限,导致rac节点2数据库宕掉,sqlplus下打开数据库报错如下: [oracle@node2 ~]$ sqlplus ...
- Oracle等待事件之Latch Free
1.产生原因 表示某个锁存器上发生了竞争.首先应该确保已经提供了足够多的Latch 数,如果仍然发生这种等待事件,那么应该进一步确定是那种锁存器上发生了竞争(在v$session_wait 上的P2 ...
- Linux eventfd分析
2017-07-20 eventfd在linux中是一个较新的进程通信方式,和信号量等不同的是event不仅可以用于进程间的通信,还可以用户内核发信号给用户层的进程.eventfd在virtIO后端驱 ...
- scrapy之定制命令
单爬虫运行 import sys from scrapy.cmdline import execute if __name__ == '__main__': execute(["scrapy ...
- java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务
一.版本限制 参看地址:http://www.mybatis.org/spring/ 二.使用入门 2.1.pom <dependency> <groupId>org.myba ...