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中Session的概念
Oct 09 PHP
一些PHP写的小东西
Dec 06 PHP
适用于php-5.2 的 php.ini 中文版[金步国翻译]
Apr 17 PHP
提高PHP性能的编码技巧以及性能优化详细解析
Aug 24 PHP
PHP内置的Math函数效率测试
Dec 01 PHP
php生成验证码函数
Oct 20 PHP
php仿微信红包分配算法的实现方法
May 13 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
Feb 17 PHP
php插件Xajax使用方法详解
Aug 31 PHP
thinkPHP中钩子的使用方法实例分析
Nov 16 PHP
PHP自定义递归函数实现数组转JSON功能【支持GBK编码】
Jul 17 PHP
PHP+Redis事务解决高并发下商品超卖问题(推荐)
Aug 03 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原理的opcodes(操作码)
2010/10/26 PHP
windows下配置php5.5开发环境及开发扩展
2014/12/25 PHP
原生js实现查找/添加/删除/指定元素的class
2013/04/12 Javascript
JS获取url链接字符串 location.href
2013/12/23 Javascript
js实现简单鼠标跟随效果的方法
2015/04/10 Javascript
jQuery实现带动画效果的多级下拉菜单代码
2015/09/08 Javascript
详解AngularJS中的表单验证(推荐)
2016/11/17 Javascript
Vue过滤器的用法和自定义过滤器使用
2017/02/08 Javascript
PHP实现本地图片上传和验证功能
2017/02/27 Javascript
vue底部加载更多的实例代码
2018/06/29 Javascript
详解JavaScript中操作符和表达式
2018/09/12 Javascript
解决echarts的多个折现数据出现坐标和值对不上的问题
2018/12/28 Javascript
JavaScript碎片—函数闭包(模拟面向对象)
2019/03/13 Javascript
详解如何探测小程序返回到webview页面
2019/05/14 Javascript
jQuery 淡入/淡出效果函数用法分析
2020/05/19 jQuery
详解Vue+elementUI build打包部署后字体图标丢失问题
2020/07/13 Javascript
[02:48]DOTA2超级联赛专访海涛:你们的选择没有错
2013/06/07 DOTA
[01:00:35]2018DOTA2亚洲邀请赛3月30日B组 EffcetVSMineski
2018/03/31 DOTA
[41:41]TFT vs Secret Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
Python多层嵌套list的递归处理方法(推荐)
2016/06/08 Python
Python编程求质数实例代码
2018/01/31 Python
python实现远程通过网络邮件控制计算机重启或关机
2018/02/22 Python
Python实现读写INI配置文件的方法示例
2018/06/09 Python
python判断一个数是否能被另一个整数整除的实例
2018/12/12 Python
Python print不能立即打印的解决方式
2020/02/19 Python
python如何实现DES加密
2020/09/21 Python
python使用ctypes库调用DLL动态链接库
2020/10/22 Python
calendar在python3时间中常用函数举例详解
2020/11/18 Python
巧用CSS3的calc()宽度计算做响应模式布局的方法
2018/03/22 HTML / CSS
Clarks鞋美国官网:全球领军鞋履品牌
2017/05/13 全球购物
亚历山大·王官网:Alexander Wang
2017/06/23 全球购物
汉森批发:Hansen Wholesale
2018/05/24 全球购物
档案工作汇报材料
2014/08/21 职场文书
材料采购员岗位职责
2015/04/03 职场文书
给男朋友的道歉短信
2015/05/12 职场文书
python 如何用map()函数创建多线程任务
2021/04/07 Python