购物车浏览商品界面代码
<!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的使用的更多相关文章

  1. Session小案例-----简单购物车的使用

    Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...

  2. java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)

    1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...

  3. 一个简单的C#获取Session、设置Session类文件

    一个简单的C#获取Session.设置Session类文件,本类主要实现大家最常用的两个功能: 1.GetSession(string name)根据session名获取session对象: 2.Se ...

  4. 简单购物车程序(Python)

    #简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...

  5. python实现简单购物车系统(练习)

    #!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('comput ...

  6. Python实例---简单购物车Demo

    简单购物车Demo # version: python3.2.5 # author: 'FTL1012' # time: 2017/12/7 09:16 product_list = ( ['Java ...

  7. 用Python实现简单购物车

    作业二:简单购物车# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,# 如果输入为空或其他非法输入则要求用户重新输入 shopping_list = [] w ...

  8. Session 简单购物车

    package session.test; import java.io.IOException; import java.io.PrintWriter; import java.util.Linke ...

  9. 2017.9.28 web设计简单的购物车应用案例--session的简单应用

    该购物过程是在session范围内完成的,需要使用session对象实现信息的共享 (1)购买“肉类”商品的页面 <%@ page language="java" impor ...

随机推荐

  1. Fluent Nhibernate Mapping for Sql Views

    Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping ...

  2. 170515、mybatis批量操作

    //Java代码 public void batchAdd(){ SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); Stud ...

  3. JS事件监听的添加方法

    一. 我们一般在的事件添加时是这样做的: elm.onclick = function( ) { //handler } 这样的写法兼容主流的浏览器,但是存在一个问题,当同一个elm绑定多个事件时,只 ...

  4. JAVAWEB Filter使用

    Filter学习 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator ...

  5. CodeForces 25C(Floyed 最短路)

    F - Roads in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  6. html锚点的作用和js选项卡锚点跳转的使用

    location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location. ...

  7. SpringCloud 进阶之Hystrix(断路器)

    1. Hystrix 断路器 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败, 比如超时,异常等,Hystrix能够保证在一个依赖出问题的情况 ...

  8. DZNEmptyDataSet 使用

    gitHub地址:https://github.com/dzenbot/DZNEmptyDataSet 效果图: 代码: #import "UIScrollView+EmptyDataSet ...

  9. java-mybaits-00103-入门程序原生的【查、增、删、改】

    一.需求 实现以下功能: 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户 更新用户 删除用户 二.具体步骤 1.增加pom引用 2.增加log4j.properties # ...

  10. IDEA阿里巴巴Java开发规约插件使用

    前言 2017年2月9日,首次公布<阿里巴巴Java开发手册>; 2017年9月25日,阿里巴巴集团发布了<阿里巴巴Java开发手册>PDF终极版; 2017年10月14日,在 ...