简体   繁体   中英

Php add to shopping cart problem

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 eg

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++;
        }
    }

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.

function add_item ($id, $qty)
    {
        $count=$this->countItems;
        echo  "uytfdgghjkl;kj<br>";
        $added = false;
        if($count>0)
        {
            for($i=0; $i < $count; $i++)
            {
                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;
                    break;
                }
            }

        }
        if(!$added)
        {
            $this->items[$this->countItems] = new OrderItem($id, $qty);
            //$this->total = $total+ ($qty *$price);
            $this->countItems++;
        }
    }

An even better option would be to use a dictionary

ie.

$arr = array();
$arr['item_id'] = new OrderItem(...);

Then you can check if the item is in the array using:

if(isset($arr[$id])){
    ...
}

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:

if($added == true) // if this cart item's quantity was incremented
{
    break;
}
else // add item
{
    $this->items[$this->countItems] = new OrderItem($id, $qty);
    // etc...
}

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 via for (instead of a while) using $count .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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