如何在购物车中添加多个商品?
这是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>
这是add_item
类中的cart
方法,
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()
);
}
}
所以,在课程中已经有一个项目之后,
array
0 =>
array
'unique_id' => string '1386638969582999' (length=16)
'quantity_request' => int 1
'quantity_stock' => string '10' (length=2)
'time_created' => int 1322429496
当我提交另一个像这样的项目
时,数组应该增加另一个项目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中,
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
但是我总是只用增加的请求数量来获得一个项目,
array
0 =>
array
'unique_id' => string '1386638969582999' (length=16)
'quantity_request' => int 2
'quantity_stock' => string '10' (length=2)
'time_created' => int 1322429496
请注意,它现在在'quantity_request' => int 2
quantity_request
只有在再次点击相同的unique_id
时才会增加。
我怎样才能做到这一点?
修改
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中,
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()
);
}
}
}
答案 0 :(得分:0)
您在开始时将$ i重置为0,从而使$ i ++无关紧要,因此当您添加另一个条目时,它会覆盖旧条目。试试$ this-&gt; content [],这样它就会添加到最后一个。