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 相关文章推荐
超强分页类2.0发布,支持自定义风格,默认4种显示模式
Jan 02 PHP
php字符串截取中文截取2,单字节截取模式
Dec 10 PHP
fleaphp下不确定的多条件查询的巧妙解决方法
Sep 11 PHP
解析php mysql 事务处理回滚操作(附实例)
Aug 05 PHP
ThinkPHP3.1新特性之动态设置自动完成和自动验证示例
Jun 19 PHP
php之readdir函数用法实例
Nov 13 PHP
PHP中array_slice函数用法实例详解
Nov 25 PHP
php多次include后导致全局变量global失效的解决方法
Feb 28 PHP
Laravel最佳分割路由文件(routes.php)的方式
Aug 04 PHP
PHP经典算法集锦【经典收藏】
Sep 14 PHP
PHP生成二维码与识别二维码的方法详解【附源码下载】
Mar 07 PHP
零基础php编程好学吗
Oct 11 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
全国FM电台频率大全 - 29 青海省
2020/03/11 无线电
PHP中如何判断AJAX提交的数据
2012/02/05 PHP
PHP中new static()与new self()的比较
2016/08/19 PHP
php实现微信模拟登陆、获取用户列表及群发消息功能示例
2017/06/28 PHP
ThinkPHP5框架中使用JWT的方法示例
2020/06/03 PHP
关于Ext中form移除textfield方法:hide(),setVisible(false),remove()
2010/12/02 Javascript
自制基于jQuery的智能提示插件一枚
2011/02/18 Javascript
jQuery判断数组是否包含了指定的元素
2015/03/10 Javascript
jQuery实现带幻灯的tab滑动切换风格菜单代码
2015/08/27 Javascript
js密码强度实时检测代码
2016/03/02 Javascript
AngularJS 如何在控制台进行错误调试
2016/06/07 Javascript
JavaScript中Form表单技术汇总(推荐)
2016/06/26 Javascript
AngularJS入门教程之Cookies读写操作示例
2016/11/02 Javascript
解决Vue不能检测数组或对象变动的问题
2018/02/24 Javascript
微信小程序实现的一键复制功能示例
2019/04/24 Javascript
浅析JS中NEW的实现原理及重写
2020/02/20 Javascript
对于Python的框架中一些会话程序的管理
2015/04/20 Python
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
2017/05/08 Python
Python实现多并发访问网站功能示例
2017/06/19 Python
Python正确重载运算符的方法示例详解
2017/08/27 Python
Python高级特性切片(Slice)操作详解
2018/09/27 Python
python实现简单图片物体标注工具
2019/03/18 Python
Python使用Pickle模块进行数据保存和读取的讲解
2019/04/09 Python
python实现人工智能Ai抠图功能
2019/09/05 Python
Python-jenkins 获取job构建信息方式
2020/05/12 Python
html5 sessionStorage会话存储_动力节点Java学院整理
2017/07/06 HTML / CSS
美国第一香水网站:Perfume.com
2017/01/23 全球购物
查找廉价航班和发现新目的地:Kiwi.com
2019/02/25 全球购物
阿尔卡特(中国)的面试题目
2014/08/20 面试题
毕业生怎样写好自荐信
2013/11/11 职场文书
纪念九一八事变演讲稿1000字
2014/09/14 职场文书
践行三严三实心得体会
2014/10/13 职场文书
2015年教师工作总结范文
2015/03/31 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
养成教育主题班会
2015/08/13 职场文书
详解CSS不定宽溢出文本适配滚动
2021/05/24 HTML / CSS