php购物车实现方法,php购物车实现_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php购物车实现方法,php购物车实现_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4734字,纯文字阅读大概需要7分钟。
内容图文
![php购物车实现方法,php购物车实现_PHP教程](/upload/InfoBanner/zyjiaocheng/186/60ab351a1b604b7897496778710d71d3.jpg)
php购物车实现方法,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];
}
?>
<?php if ($_POST[ordered]) { ?>
已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } else { ?>
添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } ?>
<?php if ($_POST[ordered]) { ?>
<?php echo $curr_product[name]; ?>
添加至购物篮成功
返回 商品列表页面.
<?php } else { ?>
添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } ?>
查看购物车的商品,代码如下:
代码如下:<?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(); //载入物品列表
?>
欢迎进入网上商店
<?php
if ($_SESSION[cart][num_items]) { // If there is something to show
?>
当前在购物篮里的物品
商品名称 | 商品说明 | 单价 | 数量 | |
---|---|---|---|---|
<?php echo $master_products_list[$product_id][name]; ?> | <?php echo $master_products_list[$product_id][desc]; ?> | <?php echo $master_products_list[$product_id][price]; ?> | | |
合计: | RMB:<?php echo $total; ?> |
<?php
}
?>
商店待出售的商品
我们提供以下商品待售:
商品名称 | 商品说明 | 单价 | |
---|---|---|---|
<?php echo $item[name]; ?> | <?php echo $item[desc]; ?> | $<?php echo $item[price]; ?> | "> 添加至购物篮 |
修改购物车的数量,代码如下:
代码如下:<?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);
?>
数量修改
将数量: <?php echo $old_num; ?> 更改为
<?php echo $_POST[quantity]; ?>
返回 商品列表页面.
功能页面,用户把购物车里面的内容保存到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程序设计有所帮助。
http://www.bkjia.com/PHPjc/936801.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/936801.htmlTechArticlephp购物车实现方法,php购物车实现 本文实例讲述了php购物车实现方法。分享给大家供大家参考。具体分析如下: 这里我们为你提供个简单...
内容总结
以上是互联网集市为您收集整理的php购物车实现方法,php购物车实现_PHP教程全部内容,希望文章能够帮你解决php购物车实现方法,php购物车实现_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。