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 23 PHP
php将数据库中的电话号码读取出来并生成图片
Aug 31 PHP
php中根据某年第几天计算出日期年月日的代码
Feb 24 PHP
PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
Apr 17 PHP
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
Apr 13 PHP
php通过session防url攻击方法
Dec 10 PHP
Thinkphp自定义代码生成工具及用法说明(附下载地址)
May 27 PHP
PHP批量修改文件名称的方法分析
Feb 27 PHP
php nginx 实时输出的简单实现方法
Jan 21 PHP
PHP重置数组为连续数字索引的几种方式总结
Mar 12 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
Sep 03 PHP
tp5使用layui实现多个图片上传(带附件选择)的方法实例
Nov 17 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
PHP4引用文件语句的对比
2006/10/09 PHP
PHP 数组入门教程小结
2009/05/20 PHP
php笔记之常用文件操作
2010/10/12 PHP
PHP register_shutdown_function函数的深入解析
2013/06/03 PHP
PHP5各个版本的新功能和新特性总结
2014/03/16 PHP
php将图片保存入mysql数据库失败的解决方法
2014/12/27 PHP
php文件扩展名判断及获取文件扩展名的N种方法
2015/09/12 PHP
php实现分页显示
2015/11/03 PHP
利用PHP计算有多少小于当前数字的数字方法示例
2020/08/26 PHP
jquery.lazyload  实现图片延迟加载jquery插件
2010/02/06 Javascript
JS实现点击按钮获取页面高度的方法
2015/11/02 Javascript
再谈JavaScript异步编程
2016/01/27 Javascript
confirm确认对话框的实现方法总结
2016/06/17 Javascript
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
2016/12/14 Javascript
JavaScript 中 JSON.parse 函数 和 JSON.stringify 函数
2018/12/05 Javascript
Python中的匿名函数使用简介
2015/04/27 Python
详谈Python2.6和Python3.0中对除法操作的异同
2017/04/28 Python
11月编程语言排行榜 Python逆袭C#上升到第4
2017/11/15 Python
python发送邮件脚本
2018/05/22 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
利用python清除移动硬盘中的临时文件
2020/10/28 Python
如何通过python计算圆周率PI
2020/11/11 Python
html5本地存储 localStorage操作使用详解
2016/09/20 HTML / CSS
加拿大票务网站:Ticketmaster加拿大
2017/07/17 全球购物
荷兰多品牌网上鞋店:Stoute Schoenen
2017/08/24 全球购物
公司培训欢迎词
2014/01/10 职场文书
快餐店的创业计划书范文
2014/01/29 职场文书
幼儿园教师岗位职责
2014/03/17 职场文书
企业安全生产标语
2014/06/06 职场文书
我的中国梦演讲稿800字
2014/08/19 职场文书
党的群众路线教育实践活动自我剖析材料
2014/10/08 职场文书
11.9消防日宣传标语
2014/10/08 职场文书
2014年变电站工作总结
2014/12/19 职场文书
小学端午节活动总结
2015/02/11 职场文书
浅谈哪个Python库才最适合做数据可视化
2021/06/28 Python
Python进行区间取值案例讲解
2021/08/02 Python