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 不错的学习资料
Feb 06 PHP
PHP 文本文章分页代码 按标记或长度(不涉及数据库)
Jun 07 PHP
thinkphp模板输出技巧汇总
Nov 24 PHP
PHP session文件独占锁引起阻塞问题解决方法
May 12 PHP
PHP中的一些常用函数收集
May 26 PHP
Yii使用DeleteAll连表删除出现报错问题的解决方法
Jul 14 PHP
golang与php实现计算两个经纬度之间距离的方法
Jul 22 PHP
Yii2简单实现多语言配置的方法
Jul 23 PHP
PHP异常处理定义与使用方法分析
Jul 25 PHP
php插件Xajax使用方法详解
Aug 31 PHP
laravel5.6 框架邮件队列database驱动简单demo示例
Jan 26 PHP
php 的多进程操作实践案例分析
Feb 28 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模拟post上传图片实现代码
2016/06/24 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
在html页面上拖放移动标签
2010/01/08 Javascript
javascript 判断字符串是否包含某字符串及indexOf使用示例
2013/10/18 Javascript
jQuery设置div一直在页面顶部显示的方法
2013/10/24 Javascript
javascript搜索框点击文字消失失焦时文本出现
2014/09/18 Javascript
jQuery制作简洁的多级联动Select下拉框
2014/12/23 Javascript
使用js获取地址栏参数的方法推荐(超级简单)
2016/06/14 Javascript
jQuery实现左侧导航模块的显示与隐藏效果
2016/07/04 Javascript
Angular.js 实现数字转换汉字实例代码
2016/07/14 Javascript
JS简单实现自定义右键菜单实例
2017/05/31 Javascript
详解vue数据渲染出现闪烁问题
2017/06/29 Javascript
nodejs 子进程正确的打开方式
2017/07/03 NodeJs
浅谈Angular路由守卫
2017/08/26 Javascript
javascript基于牛顿迭代法实现求浮点数的平方根【递归原理】
2017/09/28 Javascript
vue实现可视化可拖放的自定义表单的示例代码
2019/03/20 Javascript
Vue将props值实时传递 并可修改的操作
2020/08/09 Javascript
Nuxt的路由动画效果案例
2020/11/06 Javascript
vue中echarts的用法及与elementui-select的协同绑定操作
2020/11/17 Vue.js
vue 动态生成拓扑图的示例
2021/01/03 Vue.js
python实现的udp协议Server和Client代码实例
2014/06/04 Python
Python使用django搭建web开发环境
2017/06/09 Python
使用python实现语音文件的特征提取方法
2019/01/09 Python
如何利用Python模拟GitHub登录详解
2019/07/15 Python
tensorflow自定义激活函数实例
2020/02/04 Python
Python使用Paramiko控制liunx第三方库
2020/05/20 Python
Python使用matplotlib绘制圆形代码实例
2020/05/27 Python
马来西亚排名第一的宠物用品店:Pets Wonderland
2020/04/16 全球购物
什么是makefile? 如何编写makefile?
2013/01/02 面试题
村容村貌整治方案
2014/05/21 职场文书
爱心募捐感谢信
2015/01/22 职场文书
2015年学校医务室工作总结
2015/07/20 职场文书
《水上飞机》教学反思
2016/02/20 职场文书
JavaScript实现简单计时器
2021/06/22 Javascript
速龙x4-860k处理器相当于i几
2022/04/20 数码科技
使用MybatisPlus打印sql语句
2022/04/22 SQL Server