php添加购物车问题
我正在尝试创建一个 php 函数,将商品添加到购物车。我想要它做的是检查数组以查看该商品是否已经在那里,是否增加数量,如果没有在购物车中创建该商品。
相反,它所做的是添加一个项目,它会第一次工作(如果项目已经存在,它只会增加数量),但如果您添加另一个项目,它会继续在购物中创建该项目的新实例大车 例如,
商品 1 - 数量 4 项目 2 - 数量 1 项目 2 - 数量 1 第 2 项 - 数量 1...等等...
下面是我迄今为止拥有的代码?
function add_item ($id, $qty)
{
$count=$this->countItems;
echo "uytfdgghjkl;kj<br>";
$added = false;
if($count>0)
{
$i=0;
while($added == false)
{
echo "fghjkl<br>";
$tid = $this->items[$i]->getId();
echo "new ID: ".$tid."<br>";
echo "old ID: ".$id."<br>";
echo $i;
if($tid == $id)
{
$amount = $this->items[$i]->getQty();
$this->items[$i]->setQty($amount+1);
$added = true;
//$i++;
//break;
}
if($added == true)
{
break;
}
else //if($added == false)
{
$this->items[$this->countItems] = new OrderItem($id, $qty);
//$this->total = $total+ ($qty *$price);
$this->countItems++;
$added = true;
//break;
}
//else break;
$i++;
}
}
else
{
$this->items[$this->countItems] = new OrderItem($id, $qty);
//$this->total = $total+ ($qty *$price);
$this->countItems++;
}
}
Im trying to create a php function that adds an item to a shopping cart. what i want it to do is check the array to see if the item is already in there, if it is increase the quantity, if not create the item in the cart.
What it is doing instead is it adding an item, it'll work the first time (if the items already there it'l just increase the quantity) but if you add another item it keeps on creating new instances of that item in the shopping cart
e.g
item 1 - quantity 4
item 2 - quantity 1
item 2 - quantity 1
item 2 - quantity 1... and so on...
below is the code i have so far?
function add_item ($id, $qty)
{
$count=$this->countItems;
echo "uytfdgghjkl;kj<br>";
$added = false;
if($count>0)
{
$i=0;
while($added == false)
{
echo "fghjkl<br>";
$tid = $this->items[$i]->getId();
echo "new ID: ".$tid."<br>";
echo "old ID: ".$id."<br>";
echo $i;
if($tid == $id)
{
$amount = $this->items[$i]->getQty();
$this->items[$i]->setQty($amount+1);
$added = true;
//$i++;
//break;
}
if($added == true)
{
break;
}
else //if($added == false)
{
$this->items[$this->countItems] = new OrderItem($id, $qty);
//$this->total = $total+ ($qty *$price);
$this->countItems++;
$added = true;
//break;
}
//else break;
$i++;
}
}
else
{
$this->items[$this->countItems] = new OrderItem($id, $qty);
//$this->total = $total+ ($qty *$price);
$this->countItems++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您没有首先搜索整个数组来查看该项目是否存在于其中。下面的代码应该可以工作,但我可能犯了错字或其他错误,所以请确保仔细检查它。
更好的选择是使用字典,
即。
然后您可以使用以下命令检查该项目是否在数组中:
The problem is that you aren't searching the whole array first to see if the item is present in it. The code below should work, but I may have made a typo or something else so make sure you double check it.
An even better option would be to use a dictionary
ie.
Then you can check if the item is in the array using:
逻辑是有缺陷的。仅当该商品恰好是购物车中的第一个商品时,代码才会增加该商品的数量。否则,它将添加该项目。此处证明:
相反,您应该从循环中删除
new OrderItem($id, $qty)
,并在循环遍历所有购物车商品后检查$added
的值。为此,您需要使用$count
通过for
(而不是 while)循环。The logic is flawed. The code will increment the item's quantity only if it happens to be the first item in the cart. Otherwise, it will add the item. Evidenced here:
Instead, you should remove the
new OrderItem($id, $qty)
from the loop and check the value of$added
after looping through all the cart items. For this to work, you need to loop viafor
(instead of a while) using$count
.产品类扩展 CI_Controller{
}
class Products extends CI_Controller{
}