简体   繁体   English

购物车:将更多商品添加到购物车数组

[英]Shopping cart: adding more items into the cart array

How can I add more than one items into a shopping cart? 如何在购物车中添加多个物品?

This is the html form, 这是html表单,

<form action="cart.php?action=add" method="post" id="form-cart">
    <input type="text" name="cart[unique_id]" value="<?php echo $product->unique_id;?>" size="3" maxlength="3" />
    <input type="text" name="cart[quantity_stock]" value="<?php echo $product->in_stock;?>" size="3" maxlength="3" />
    <input type="submit" value="Add to cart" />
</form>

this is the add_item method in the cart class, 这是cart类中的add_item方法,

 public function add_item($info)
{

    # Check if the content array does not exist yet.
    if (isset($this->content[0]['unique_id'])) 
    {
        $i = 0;
        $this->content[$i]['unique_id'] = $this->content[$i]['unique_id'];
        $this->content[$i]['quantity_request']++;
        $this->content[$i]['quantity_stock'] = $this->content[$i]['quantity_stock'];
        $this->content[$i]['time_created'] = time();
        $i += $i;

        # Check if the quantity request exceeds quanity in stock.
        if($this->content[$i]['quantity_request'] > $this->content[$i]['quantity_stock'])
        {
            # If it does, then set the quantity request to quanity in stock.
            $this->content[$i] = array(
                'unique_id' => $this->content[$i]['unique_id'], 
                'quantity_request' => $this->content[$i]['quantity_stock'], 
                'quantity_stock' => $this->content[$i]['quantity_stock'],
                'time_created' => time(),
                'error' => 'The quantity you requested on this item is exceeding the quantity in our stock.'
            );
        }
    } 
    else 
    {
        //$this->content[$unique_id] = array('quantity_request' => 1, 'quantity_stock' => $this->content[$unique_id]['quantity_stock'],'time_created' => time());

        # Create the content array, $unique_id as the key.
        # If it does, then set the quantity request to quanity in stock.
        $this->content[0] = array(
            'unique_id' => $info['unique_id'],
            'quantity_request' => 1,
            'quantity_stock' => $info['quantity_stock'],
            'time_created' => time()
        );
    }

}

So, after I have an item already in the class, 所以,当我已经在课堂上有一个项目,

array
  0 => 
    array
      'unique_id' => string '1386638969582999' (length=16)
      'quantity_request' => int 1
      'quantity_stock' => string '10' (length=2)
      'time_created' => int 1322429496

The array should have increased another item when I submit another item like this, 当我提交这样的另一个项目时,数组应该增加了另一个项目,

html, html,

<form action="cart.php?action=add" method="post" id="form-cart">
    <input type="text" name="cart[<?php echo $product->unique_id;?>][quantity_stock]" value="<?php echo $product->in_stock;?>" size="3" maxlength="3" />
    <input type="submit" value="Add to cart" />
</form>

php, php,

array
  0 => 
    array
      'unique_id' => string '1386638969582999' (length=16)
      'quantity_request' => int 1
      'quantity_stock' => string '11' (length=2)
      'time_created' => int 1322429496

  1 => 
    array
      'unique_id' => string '1386638969582900' (length=16)
      'quantity_request' => int 1
      'quantity_stock' => string '10' (length=2)
      'time_created' => int 1322429400

But I always get one item only with the increasing requested quantity, 但是我总是只能得到增加数量的一件商品,

array
      0 => 
        array
          'unique_id' => string '1386638969582999' (length=16)
          'quantity_request' => int 2
          'quantity_stock' => string '10' (length=2)
          'time_created' => int 1322429496

Note that it now became 2 in 'quantity_request' => int 2 请注意,现在变成了2 'quantity_request' => int 2

The quantity_request should only increased if the same unique_id is clicked again. quantity_request如果同一只增加unique_id再次点击。

How can I make this right? 我该怎么做呢?

EDIT: 编辑:

html, html,

<form action="cart.php?action=add" method="post" id="form-cart">
    <input type="text" name="cart[<?php echo $product->unique_id;?>][quantity_stock]" value="<?php echo $product->in_stock;?>" size="3" maxlength="3" />
    <input type="submit" value="Add to cart" />
</form>

php, php,

public function add_item($item_array)
    {

        foreach ($item_array as $unique_id => $info)
        {

            # Check if the content array does not exist yet.
            if (isset($this->content[$unique_id])) 
            {
                $this->content[$unique_id]['quantity_request']++;
                $this->content[$unique_id]['quantity_stock'] = $this->content[$unique_id]['quantity_stock'];
                $this->content[$unique_id]['time_created'] = time();

                # Check if the quantity request exceeds quanity in stock.
                if($this->content[$unique_id]['quantity_request'] > $this->content[$unique_id]['quantity_stock'])
                {
                    # If it does, then set the quantity request to quanity in stock.
                    $this->content[$unique_id] = array(
                        'quantity_request' => $this->content[$unique_id]['quantity_stock'], 
                        'quantity_stock' => $this->content[$unique_id]['quantity_stock'],
                        'time_created' => time(),
                        'error' => 'The quantity you requested on this item is exceeding the quantity in our stock.'
                    );
                }
            } 
            else 
            {
                //$this->content[$unique_id] = array('quantity_request' => 1, 'quantity_stock' => $this->content[$unique_id]['quantity_stock'],'time_created' => time());

                # Create the content array, $unique_id as the key.
                # If it does, then set the quantity request to quanity in stock.
                $this->content[$unique_id] = array(
                    'quantity_request' => 1,
                    'quantity_stock' => $info['quantity_stock'],
                    'time_created' => time()
                );
            }

        }
    }

You reset your $i to 0 at the start, thus making the $i++ irrelevant, so when you add another entry, it overwrites the old. 您在开始时将$ i重置为0,从而使$ i ++不相关,因此,当您添加另一个条目时,它将覆盖旧的条目。 Try $this->content[] so it just adds to the last. 尝试$ this-> content [],以便将其添加到最后一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM