购物车:将更多商品添加到购物车数组中
如何将多件商品添加到购物车中?
这是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>
这是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()
);
}
}
所以,当我在类中已有一个项目后,
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' => 中变成了 2 。 int 2
仅当再次单击相同的 unique_id
时,quantity_request
才会增加。
我怎样才能做到这一点?
编辑:
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()
);
}
}
}
How can I add more than one items into a shopping cart?
This is the html form,
<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,
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,
<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
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
The quantity_request
should only increased if the same unique_id
is clicked again.
How can I make this right?
EDIT:
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()
);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在开始时将 $i 重置为 0,从而使 $i++ 变得无关紧要,因此当您添加另一个条目时,它会覆盖旧的条目。尝试 $this->content[] 这样它就添加到最后一个。
You reset your $i to 0 at the start, thus making the $i++ irrelevant, so when you add another entry, it overwrites the old. Try $this->content[] so it just adds to the last.