在 PHP 中的单独行上显示具有不同选项的每个产品 [英] Display each product with different options on separate line in PHP

查看:21
本文介绍了在 PHP 中的单独行上显示具有不同选项的每个产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个购物车,可以让您选择 JAR 文件,然后选择 JAR 文件的内容(即选项).

当产品被添加到购物车"时,运行以下脚本:

$productID = $_POST['id'];$action = $_POST['action'];if(!$_POST['id'] | !$_POST['action']){$productID = $_GET['id'];$action = $_GET['action'];}//创建为项目选择的选项数组.$optionsSelected = array();if (!empty($_POST['productOption'])){foreach ($_POST['productOption'] 作为 $options)$optionsSelected[] = $options;开关($动作){案例添加":$_SESSION['购物车'][$productID]++;$_SESSION['options'.$productID]= $optionsSelected;header('位置:/商店/购物车');休息;案例删除":$_SESSION['购物车'][$productID]--;取消设置($_SESSION['options'.$productID]);if($_SESSION['购物车'][$productID] == 0){未设置($_SESSION['购物车'][$productID]);取消设置($_SESSION['options'.$productID]);}header('位置:/商店/购物车');休息;}

然后就这样在购物车里显示出来了:

<?php if (count($_SESSION['options'.$id]) > 0): ?><?php foreach($_SESSION['options'.$id] as $key => $options): ?><p><?php echo $options;?></p><?php endforeach;?><?php endif;?>

(这只是选项的部分.要查看完整的购物车页面代码点击此处(推荐))

现在,如果您使用不同的选项将具有相同 ID 的另一个产品添加到购物车,则选项的会话将被覆盖.第一组选项不见了!

我想让购物车意识到已经为 ID 设置了一个选项会话,它需要做一些不同的事情来在购物车中显示一个全新的行来显示它并保留两个选项集产品编号.要查看该网站,请访问 candykingdom.org/shop/products.php..>

我不知道从哪里开始.几天来我一直在寻找并盯着我的屏幕,但我什么也没找到.我怎样才能做到这一点?

解决方案

您需要单独存储它们,而不管产品 ID 是什么.也许是这样的

$key = $productID .":" .base64_encode(序列化($optionsSelected));$_SESSION['购物车'][$key]++;

这很容易在另一端逆转:

foreach ($_SESSION['cart'] as $key => $quantity) {$product = expand(':', $key);$productId = $product[0];$optionsSelected = unserialize(base64_decode($product[1]));}

So, I have a shopping cart that allows you to select a JAR file and then select the contents of the JAR file (i.e. options).

When the product is "added to the cart" the following script is ran:

$productID = $_POST['id'];

$action = $_POST['action'];

if(!$_POST['id'] | !$_POST['action'])
{
    $productID = $_GET['id'];

    $action = $_GET['action'];
}

// Creates the array of the options chosen for the item.
$optionsSelected = array();
if (!empty($_POST['productOption']))
{
   foreach ($_POST['productOption'] as $options)
      $optionsSelected[] = $options;

switch($action) {
   case "add":
      $_SESSION['cart'][$productID]++;
      $_SESSION['options'.$productID]= $optionsSelected;
      header('location: /shop/cart');

   break;

   case "remove":

      $_SESSION['cart'][$productID]--;
      unset($_SESSION['options'.$productID]);
      if($_SESSION['cart'][$productID] == 0)
      {
          unset($_SESSION['cart'][$productID]);
          unset($_SESSION['options'.$productID]);
      }
      header('location: /shop/cart');
   break;
}

And then it is displayed out like so in the shopping cart:

<?php if (count($_SESSION['options'.$id]) > 0): ?>
   <?php foreach($_SESSION['options'.$id] as $key => $options): ?>
       <p><?php echo $options; ?></p>
   <?php endforeach; ?>
<?php endif; ?>

(This is only the portion for the options. To see the full shopping cart page code click here (recommended))

Right now, the session for the options get overwritten if you go and add another product with the same ID to the cart with different options. And that first set of options is gone!

I want to make the shopping cart realize that there is already a session of options set for the ID and that it needs to make something different to display an entirely new line in the shopping cart to display it and preserve both option sets for the product id. To see the website, visit candykingdom.org/shop/products.php.

I have no idea where to go from here. I have been searching and staring at my screen for days, but I haven't gotten anywhere. How can I accomplish this?

解决方案

You need to store them separately, regardless of a common product ID. Maybe something like this

$key = $productID . ":" . base64_encode(serialize($optionsSelected));
$_SESSION['cart'][$key]++;

This can easily be reversed on the other end:

foreach ($_SESSION['cart'] as $key => $quantity) {
  $product = explode(':', $key);
  $productId = $product[0];
  $optionsSelected = unserialize(base64_decode($product[1]));
}

这篇关于在 PHP 中的单独行上显示具有不同选项的每个产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆