dphw5101 2013-03-29 23:38
浏览 53
已采纳

PHP简单的购物车功能与文本文件

I'm working on a basic shopping cart using a text file (just for educational purposes). It's formatted with one product on each line, like so:

Product name|Price|Quantity 
Product name|Price|Quantity 
Product name|Price|Quantity 

How would I accomplish an addToCart() function that looks through the cart.txt file and either adds a product to a new line with a quantity of 1 if it's not already in the cart, or adds 1 to the quantity of that product if it's already in the cart?

  • 写回答

3条回答 默认 最新

  • douxigai8757 2013-03-30 01:43
    关注

    You could stick with the same format, but add an ID column, so you'd have this:

    ProductID|Product name|Price|Quantity 
    ProductID|Product name|Price|Quantity 
    ProductID|Product name|Price|Quantity 
    

    Then use the ID field as the array key. You could use a similar approach with the product name, but you'd want to clean any spaces or special characters out.

      $raw = file_get_contents($path);
      $raw = explode("
    ", $raw);
      $data = array();
      foreach ($raw as $d) {
        $d = explode('|', $d);
        // the left-most data will be our key
        $key = array_shift($d);
        $data[$key] = $d;
      }
    

    Now you'd have an array like this (for example):

    array(
      5=>array(
        'Widget id #5',
        '5.00',
        '2'
      ),
      11=>array(
        'Widget id #11',
        '6.00',
        '1'
      )
    )
    

    An even easier way would be to use JSON for the file format. That way, you don't have to monkey with parsing the data after you get it out of the file, and associative keys are easier to implement. Either way you do it, you'd follow the same steps:

    • Get the data from the file and into a variable
    • See if the product is already in the cart
      • if not, add it
    • Increment the quantity by 1 (or any other number, really)
    • Write the data back into a file

    Using JSON, it would look something like this:

    $path = 'path/to/data.txt';
    $data = json_decode(file_get_contents($path), true);
    
    // if the product isn't in the cart, add it
    if (array_key_exists($product_id, $data) === false) {
    
      // retrieve the product information for $product_id
      // populate $product_name and $product_price
    
      $data[$product_id] = array(
        'name'=>$product_name,
        'quantity'=>0,
        'price'=>$product_price
      );
    }
    
    // increment the quantity
    $data[$product_id]['quantity']++;  // $data[$product_id][2]++ if you didn't use JSON
    
    // write the data back into the file
    file_put_contents($path, json_encode($data));
    

    Documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致