使用php怎么编写一个购物车功能
创始人
2024-12-12 16:41:56
0次

 代码如下:

//
// 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];
}
?>


    <br/>    <?php if ($_POST[ordered]) {  ?> <br/>        已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮 <br/>    <?php } else {  ?> <br/>        添加 <?php echo $curr_product[name]; ?> 到您的购物篮 <br/>    <?php } ?> <br/>   



   


        添加至购物篮成功


 
    返回 商品列表页面.

   

添加 到您的购物篮


 
   

    商品名称:
   

    商品说明:
   

    商品单价: RMB
   

    商品数量:
   
   
   
   



查看购物车的商品,代码如下:

复制代码 代码如下:

//
// 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(); //载入物品列表
?>


    演示会话跟踪的购物篮程序

 

 

欢迎进入网上商店


 
if ($_SESSION[cart][num_items]) {  // If there is something to show
?>

当前在购物篮里的物品






   
   
   
   
   

  
    // 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];
?>

   
   
   
   
   

    }
?>

   
   
 


        商品名称
   

        商品说明
   

        单价
   

        数量
   
 
        
   

       
   

       
   

       
   

       

       
                        value="">
   

       
       
   

       合计:
   

        RMB:
   
 





}
?>
 

商店待出售的商品





    我们提供以下商品待售:





   
   
   
   

    // Show all of the products
    foreach ($master_products_list as $product_id => $item) {
?>

   
   
   
   

    }
 
?>

        商品名称
   

        商品说明
   

        单价
   
 
        
   

       
   

       
   

        $
   

       
            添加至购物篮
       

   

修改购物车的数量,代码如下:

复制代码 代码如下:

//
// 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);
?>
 


    <br/>        数量修改 <br/>   


   

将数量: 更改为
        


    返回 商品列表页面.

功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:

复制代码 代码如下:

//物品数组
$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 文件失败");
}
?>


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...