开发者

Problems in php to add products in a shop cart

开发者 https://www.devze.com 2023-04-06 19:25 出处:网络
I have these 3 classes: shopping cart->orders->products. First I add a product, when I add the second, I compare with other products in the array products in the objec开发者_如何学Got orders. if coun

I have these 3 classes: shopping cart->orders->products.

First I add a product, when I add the second, I compare with other products in the array products in the objec开发者_如何学Got orders. if count($product)==0, then add the firs product if count>0, I compare the id´s of the products in the array with the new product I want to add :

    public function setProduct($product,$new_quantity){
        if($this->num_product == 0) {
            $this->product[$this->num_product]=$product;
            $this->num_product++;
        } else if($this->num_product > 0) {
            for($i=0; $i < $this->num_product; $i++) {
                if($this->product[$i]->getIdproduct() == $product->getIdproduct()) {
                    $this->product[$i]->setQuantity($this->product[$i]->getquantity()+$new_quantity);
                    break;
                } else {
                    continue;
                }
                $this->product[$this->num_product]=$product;
                $this->num_product++;
            }

        }
    }

When I finish the comparison, I have to add these new product but this doesn't work. What is my mistake?


Your method is really complicated for what you want to do.

First of all, $this->num_product is exactly the same thing as count($this->product). So why use a variable to hold this information ?

Second of all, no need to separate the case of zero products and some products in the cart, the for loop won't execute if there's no product in the cart.

I propose this as a solution :

public function setProduct($product,$new_quantity){
   for($i=0; $i < count($this->product); $i++) {
        if($this->product[$i]->getIdproduct() == $product->getIdproduct()) {
            $this->product[$i]->setQuantity($this->product[$i]->getquantity() + $new_quantity);
            return; // we found our product, get out of the function.
        }
   }
   // the product was not found in the array, add it to the end
   $this->product[]=$product;
}

If you want to keep your code, I think the mistake is that you add the product to the array at each iteration of the loop (the last two lines of the loop, after the if clause), but it's really hard to say without some explanation about what you think is wrong.


The problem in your code is that the last two lines of the the for loop will never get executed. If the if contidion is true it will break the loop, otherwise will continue without ever reaching these two lines. The easiest way is to use retun if your function doesn't do anything after the loop.

 public function setProduct($product,$new_quantity){
        if($this->num_product == 0) {
            $this->product[$this->num_product]=$product;
            $this->num_product++;
        } else if($this->num_product > 0) {
            for($i=0; $i < $this->num_product; $i++) {
                if($this->product[$i]->getIdproduct() == $product->getIdproduct()) {
                    $this->product[$i]->setQuantity($this->product[$i]->getquantity()+$new_quantity);
                    return;
                }
            }
            $this->product[$this->num_product]=$product;
            $this->num_product++;
        }
    }

See also what @krtek says about the need of no checking for 0 elements

0

精彩评论

暂无评论...
验证码 换一张
取 消