php购物车实现方法


Posted in PHP onJanuary 03, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
怎样才能成为PHP高手?学会“懒惰”的编程
Dec 05 PHP
php部分常见问题总结
Mar 27 PHP
php 中文和编码判断代码
May 16 PHP
php小偷相关截取函数备忘
Nov 28 PHP
提高php运行速度的一些小技巧分享
Jul 03 PHP
分享下页面关键字抓取components.arrow.com站点代码
Jan 30 PHP
php 5.6版本中编写一个PHP扩展的简单示例
Jan 20 PHP
yii2 页面底部加载css和js的技巧
Apr 21 PHP
PHP实现的进度条效果详解
May 03 PHP
Laravel框架中缓存的使用方法分析
Sep 06 PHP
在 PHP 和 Laravel 中使用 Traits的方法
Nov 13 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
Feb 15 PHP
PHP实现格式化文件数据大小显示的方法
Jan 03 #PHP
php自定义加密与解密程序实例
Dec 31 #PHP
推荐一本PHP程序猿都应该拜读的书
Dec 31 #PHP
推荐10个提供免费PHP脚本下载的网站
Dec 31 #PHP
php使用google地图应用实例
Dec 31 #PHP
php将文本文件转换csv输出的方法
Dec 31 #PHP
19个Android常用工具类汇总
Dec 30 #PHP
You might like
php调用MySQL存储过程的方法集合(推荐)
2013/07/03 PHP
php输出反斜杠的实例方法
2019/09/19 PHP
js 页面执行时间计算代码
2009/03/04 Javascript
JQuery入门——用one()方法绑定事件处理函数(仅触发一次)
2013/02/05 Javascript
AngularJS入门教程之学习环境搭建
2014/12/06 Javascript
jQuery学习笔记之创建DOM元素
2015/01/19 Javascript
JavaScript 基本概念
2015/01/20 Javascript
基于jQuery通过jQuery.form.js插件使用ajax提交form表单
2015/08/17 Javascript
json格式数据的添加,删除及排序方法
2016/01/21 Javascript
JavaScript中的this使用详解
2016/07/27 Javascript
JavaScript中的冒泡排序法
2016/08/03 Javascript
ES6新数据结构Set与WeakSet用法分析
2017/03/31 Javascript
CKEditor扩展插件:自动排版功能autoformat插件实现方法详解
2020/02/06 Javascript
2020淘宝618理想生活列车自动领喵币js脚本的代码
2020/06/02 Javascript
Vue实现小购物车功能
2020/12/21 Vue.js
jquery实现穿梭框功能
2021/01/19 jQuery
Python数字图像处理之霍夫线变换实现详解
2018/01/12 Python
Django1.9 加载通过ImageField上传的图片方法
2018/05/25 Python
django rest framework 数据的查找、过滤、排序的示例
2018/06/25 Python
python 提取tuple类型值中json格式的key值方法
2018/12/31 Python
Python列表如何更新值
2020/05/27 Python
Python爬虫爬取百度搜索内容代码实例
2020/06/05 Python
利用CSS3的transition属性实现滑动效果
2015/08/05 HTML / CSS
html5 乒乓球(碰撞检测)实例二
2013/07/25 HTML / CSS
Farnell德国:电子元器件供应商
2018/07/10 全球购物
编程实现当输入某产品代码则打印出该产品记录的功能
2014/05/03 面试题
毕业生自荐书
2014/02/02 职场文书
承诺书的格式范文
2014/03/28 职场文书
勤俭节约演讲稿
2014/05/08 职场文书
国庆65周年演讲稿:回首往昔,展望未来
2014/09/21 职场文书
生日赠语
2015/06/23 职场文书
安全教育培训制度
2015/08/06 职场文书
严以修身专题学习研讨会发言材料
2015/11/09 职场文书
MySQL GRANT用户授权的实现
2021/06/18 MySQL
我去timi了,一起去timi是什么意思?
2022/04/13 杂记
VUE解决跨域问题Access to XMLHttpRequest at
2022/05/06 Vue.js