Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

session - Display each product with different options on separate line in PHP

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...