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程序设计有所帮助。


猜你喜欢
- 在ASP.NET中使用js时,js获取DOM元素时,经常获取不到,这是因为获取的方法有误,现在介绍一方法,解决如何使用js获取ASP.NET
- 很早就在这里看到过解决方案,与嗷嗷讨论后发现这个方案还是很可靠的。当然,唯一的缺点就是每一个属性都要去Hack,但我在很多实践中,只用‘修正
- <?php $foo = 'Bob'; // 将 'Bob' 赋给 $foo $bar = &
- js部分setInterval("time_controller()",1000);function time_cont
- 快捷键可以帮助我们有效提高效率,我们来看看网页设计软件FrontPage有哪些快捷键。相关文章:Dreamweaver快捷键大全、photo
- 简介CSS Sprites并没有一个确定的中文翻译,通常被意译为“CSS图像拼合”或“CSS贴图定位”。CSS Sprites并不是一门新技
- 两年前,我们开发了一套基于Flash的文件(主要是图片)上传RIA应用,提供给阿里巴巴的用户使用。如果你使用过Wordpress或flick
- Linux下MySQL整个数据库的备份与还原[root]# /usr/bin/mysqldump -h127.0.0.1 -uusernam
- 本文实例讲述了php实现的美国50个州选择列表。分享给大家供大家参考。具体如下:这里展示的是php生成的美国50个州的选择列表,自动选择当前
- CSS的出现使网页制作者在对网页元素的控制方便许多,当然,有利必有弊,CSS只能对颜色、大小、距离等静
- 听说最近流行JQ风格的语法,不流行EXT风格了一.//ajax类fw=window.fw||{};fw.ajax = { &nbs
- perl - Practical Extraction and Report Language,Perl有很多命令行参数,通过它可以让你的程
- 如果没有设置分页,django-rest-framework 会将所有资源类表序列化后返回,如果资源很多,就会对网站性能造成影响。为此,我们
- 本文实例讲述了JavaScript数据结构中串的表示与应用。分享给大家供大家参考,具体如下:类似于线性表的顺序存储结构,用一组地址连续的存储
- Bootstrap提供了四种用于<img>类的样式,分别是:.img-rounded:圆角 (IE8 不支持),添加 borde
- 今天开始学习 YUI,加强一下对 JavaScript 的理解。1. 命名空间 YAHOO
- 一、如何理解本条内容:一个“简单”和“复杂”的例子在我和开发人员沟通一个项目需求的时候,他们频频慨叹Mockup的设计所考虑情况之细致,很多
- 通常测试人员或公司实习人员需要处理一些txt文本内容,而此时使用Python是比较方便的语言。它不光在爬取网上资料上方便,还在NLP自然语言
- 像素误差看自己设计好上线的网站,偶尔会发觉像素行间出现了弹性空间,总在不经意间蹦出一定的差距。有些页面很难发现,比如活动类页面,这类页面多呈
- 一、Position1、语法:position:static/ absolute / fixed / relative2、参数:(1)sta