php购物车实现方法
作者:shichen2014 发布时间:2023-11-16 22:54:51
标签:php,购物车
本文实例讲述了php购物车实现方法。分享给大家供大家参考。具体分析如下:
这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容.
增加商品到购物车,代码如下:
<?php
//
// add_item.php:
// Add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}
require 'lib.inc.php'; // LoadProducts()
LoadProducts(); // Load products in $master_products_list
// Make $curr_product global
$curr_product = array();
// Loop through all the products and pull up the product
// that we are interested in
foreach ($master_products_list as $prod_id => $product) {
if (trim($prod_id) == trim($_GET[id])) {
$curr_product = $product;
}
}
// Register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已经注册";
if ($_POST[ordered]) { // If they have chosen the product
array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity]));
$_SESSION[cart][num_items] += $_POST[quantity];
}
?>
<html>
<head>
<title>
<?php if ($_POST[ordered]) { ?>
已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } else { ?>
添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } ?>
</title>
</head>
<body>
<?php if ($_POST[ordered]) { ?>
<h1><?php echo $curr_product[name]; ?>
添加至购物篮成功</h1>
<a href="cart.php">返回</a> 商品列表页面.
<?php } else { ?>
<h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1>
<form action="<?php echo $PHP_SELF; ?>" method="post">
商品名称: <?php echo $curr_product[name]; ?>
<br>
商品说明: <?php echo $curr_product[desc]; ?>
<br>
商品单价: RMB<?php echo $curr_product[price]; ?>
<br>
商品数量: <input type="text" size="7" name="quantity">
<input type="hidden" name="id" value="<?php echo $_GET[id]; ?>">
<input type="hidden" name="ordered" value="1">
<input type="submit" value="添加至购物栏">
</form>
<?php } ?>
</body>
</html>
查看购物车的商品,代码如下:
<?php
//
// cart.php:
//
session_start();
require 'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if (session_is_registered('cart')) {
session_register('cart');
}
// 如果购物篮没有初始化,则初始化购物篮
if (!isset($_SESSION[cart][num_items])) {
$_SESSION[cart] = array("num_items" => 0,
"products" => array());
}
// From site_lib.inc, Loads the $master_products_list array
LoadProducts(); //载入物品列表
?>
<html>
<head>
<title>演示会话跟踪的购物篮程序</title>
</head>
<body>
<h1>欢迎进入网上商店</h1>
<?php
if ($_SESSION[cart][num_items]) { // If there is something to show
?>
<h2>当前在购物篮里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
数量
</th>
<th>
</th>
</tr>
<?php
// Loop through the products
foreach ($_SESSION[cart][products] as $i => $product) {
$product_id = $product[0];
$quantity = $product[1];
$total += $quantity *
(double)$master_products_list[$product_id][price];
?>
<tr>
<td>
<?php echo $master_products_list[$product_id][name]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][desc]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][price]; ?>
</td>
<td>
<form action="change_quant.php" method="post">
<input type="hidden" name="id" value="<?php echo $i; ?>">
<input type="text" size="3" name="quantity"
value="<?php echo $quantity; ?>">
</td>
<td>
<input type="submit" value="数量更改">
</form>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" ALIGN="right">
<b>合计: </b>
</td>
<td colspan="2">
RMB:<?php echo $total; ?>
</td>
<td> </td>
</tr>
</table>
<br>
<br>
<?php
}
?>
<h2>商店待出售的商品</h2>
<br>
<i>
我们提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
</th>
</tr>
<?php
// Show all of the products
foreach ($master_products_list as $product_id => $item) {
?>
<tr>
<td>
<?php echo $item[name]; ?>
</td>
<td>
<?php echo $item[desc]; ?>
</td>
<td>
$<?php echo $item[price]; ?>
</td>
<td>
<a href="add_item.php?id=<?php echo $product_id; ?>">
添加至购物篮
</a>
</td>
</tr>
<?php
}
?>
</table>
修改购物车的数量,代码如下:
<?php
//
// change_quant.php:
// Change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}
// Typecast to int, making sure we access the
// right element below
$i = (int)$_POST[id];
// Save the old number of products for display
// and arithmetic
$old_num = $_SESSION[cart][products][$i][1];
if ($_POST[quantity]) {
$_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity
} else {
unset($_SESSION[cart][products][$i]); // Send the product into oblivion
}
// Update the number of items
$_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ?
$_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) :
$_SESSION[cart][num_items] + ($_POST[quantity]-$old_num);
?>
<html>
<head>
<title>
数量修改
</title>
</head>
<body>
<h1> 将数量: <?php echo $old_num; ?> 更改为
<?php echo $_POST[quantity]; ?></h1>
<a href="cart.php">返回</a> 商品列表页面.
</body>
</html>
功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:
<?php
//物品数组
$master_products_list = array();
//载入物品数据函数
function LoadProducts() {
global $master_products_list;
$filename = 'products.txt';
$fp = @fopen($filename, "r")
or die("打开 $filename 文件失败");
@flock($fp, 1)
or die("锁定 $filename 文件失败");
//读取文件内容
while ($line = fgets($fp, 1024)) {
list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
$id = trim($id); //去掉首尾特殊符号
$master_products_list[$id] = array("name" => $name, //名称
"desc" => $desc, //说明
"price" => $price); //单价
}
@fclose($fp) //关闭文件
or die("关闭 $filename 文件失败");
}
?>
很简单,我们只用了4个文件就实现用php 做好购物车功能,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好.
希望本文所述对大家的php程序设计有所帮助。


猜你喜欢
- 本文实例讲述了Python常见数据类型转换操作。分享给大家供大家参考,具体如下:类型转换主要针对几种存储工具:list、tuple、dict
- 话不多说,直接上代码吧!import numpy as npA = np.delete(A, 1, 0) # 删除A的第二行B = np.d
- 面向对象设计与面向对象编程的关系 面向对象设计(OOD)不会特别要求面向对象编程语言。事实上,OOD 可以由纯结构化语言来实现,比
- 1 概要deque 是一个双端队列, 如果要经常从两端append 的数据, 选择这个数据结构就比较好了, 如果要实现随机访问,不建议用这个
- 本文实例讲述了Go语言中嵌入C语言的方法。分享给大家供大家参考。具体分析如下:Go语言官方带了一个工具叫cgo,可以很方便的在Go语言代码中
- 在使用爬虫爬取网络数据时,如果长时间对一个网站进行抓取时可能会遇到IP被封的情况,这种情况可以使用代理更换ip来突破服务器封IP的限制。随手
- python的列表list可以用for循环进行遍历,实际开发中发现一个问题,就是遍历的时候删除会出错,例如l = [1,2,3,4]for
- 引言作为一个web前端开发,对axios肯定不陌生,但是在前端开发中,一般是使用axios来请求后端接口,获取数据。而使用node+axio
- CAS算法(compare and swap)CAS算法涉及到三个操作数需要读写的内存值V进行比较的值A拟写入的新值B当且仅当 V 的值等于
- 游戏规则:双方轮流选择棋盘的列号放进自己的棋子,若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,则使用该型号棋子的玩家就赢了!
- union all在MySQL5.6下的表现Part1:MySQL5.6.25[root@HE1 ~]# MySQL -uroot -pEn
- Python2.7已于2020年1月1日开始停用,之前RF做自动化都是基于Python2的版本。没办法,跟随时代的脚步,我们也不得不升级以应
- 这篇文章主要介绍了Python Django 封装分页成通用的模块详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 本文实例为大家分享了pygame贪吃蛇游戏的具体代码,供大家参考,具体内容如下1.准备工作我们已经初始化了一个400*400的界面,为方便看
- 如何用表单的方式推送请求的信息?具体见下:<html><head><title>答复用户信息
- 考点:将字典转换为XML文档;将XML文档转换为字典。面试题1.面试题一:如何将一个字典转换为XML文档,并将该XML文档保存为文本文件。2
- 今天在我的Centos6.5机器上安装 Django 开发环境,在安装完使用 “django-admin.py startproject m
- 给内存和cpu使用量设置限制在linux系统中,使用Python对内存和cpu使用量设置限制需要通过resource模块来完成。resour
- 使用axios固定url请求前缀main.js中添加:使用方法:定义axios默认路径前缀或动态修改前缀如:每个请求url前都要加一个前缀,
- 如果您刚刚开始学习PHP,可能有许多函数需要研究,今天我们就来学习一下PHP Header()的使用方法,更多的使用说明,请您参照PHP中文