简单购物车的实现,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 ...
随机推荐
- css 分栏高度自动相等
方法2: <div class="ticket_table"> <div class="ticket_l"> <h3>全票& ...
- angularJS中的ng-repeat指令!
ng-repeat 指令: ng-repeat 指令用来遍历一个数组重复创建当前元素: <ul ng-app="myApp" ng-controller="myAp ...
- onethink封装arclist调用文章列表!
其实没有什么东西,做个记录,方便以后使用! <ul> <arclist mid='2' cid='2' row='2'> <li>{$title}</li&g ...
- 07.Curator计数器
这一篇文章我们将学习使用Curator来实现计数器.顾名思义,计数器是用来计数的,利用ZooKeeper可以实现一个集群共享的计数器.只要使用相同的path就可以得到最新的计数器值,这是由Zo ...
- ZOJ 3209 Treasure Map(精确覆盖)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- [Haskell]解决hslua unknown symbol `___s trtod'的问题
用cabal编译libpandoc时遇到这样的错误: HShslua-0.3.12.o: unknown symbol `___s trtod' ghc.exe: unable to load pac ...
- SQL的子查询操作
对于表中的每一个记录,我们有时候需要提取特殊的或者你需要的记录,要提前做一个表的筛选,之后再对你选出的记录做一个修改,此时你必须使用SQL的子查询操作.如:修改id=5的记录的strContent字段 ...
- Spark-RDD算子
一.Spark-RDD算子简介 RDD(Resilient Distributed DataSet)是分布式数据集.RDD是Spark最基本的数据的抽象. scala中的集合.RDD相当于一个不可变. ...
- android的selector选择器
1. drawable/actionbar_compat_item.xml 2.drawable/actionbar_compat_item_pressed.xml 3.drawable/action ...
- java爬取网页内容 简单例子(2)——附jsoup的select用法详解
[背景] 在上一篇博文java爬取网页内容 简单例子(1)——使用正则表达式 里面,介绍了如何使用正则表达式去解析网页的内容,虽然该正则表达式比较通用,但繁琐,代码量多,现实中想要想出一条简单的正则表 ...