Recently I switched to the OOP and am making a shopping cart for the site. Found a basket here - Basket

Cooks are still not very familiar, and in general with the PLO. But I want to understand.

There is a class Cart, in which there are 2 methods responsible for products

public function getItems($for_sql = false) { if ($for_sql) { return implode(',', $this->items); } return $this->items; } public function addItem($id) { $id = (int)$id; $key = array_search($id, $this->items); if (!in_array($id, $this->items)) { array_push($this->items, $id); } Cookie::set('items', serialize($this->items)); } 

And in the file cart.php I display this way:

 echo "My cart: <br>"; foreach ($items as $items) { echo "<b>{$items['name']}</b> | Количество " .count($items['id']). " | <a href='cart.php?action=delete&id={$items['id']}'>Удалить из корзины</a><br>"; 

}

Tried to display the number of repeated id through count, but also unsuccessfully. Therefore, I ask for help. How to do the following:

  1. How to add the quantity of goods, if this product is already added to the cart? At the moment, only once the product is added, and then the quantity of this product is not added (+ 1 to the quantity)

  2. How to display the total amount using this basket class?

I tried to add the function array_key_exists () to the addItem ($ id) method, but without success. The quantity does not add up.

  • According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation, add a description of the specific problems you have encountered. - Mikhail Vaysman
  • Use the Cart class, there is a getProducts method that will likely return all products added to the cart. Further, the amount can be calculated in a cycle. - Firepro
  • I use the Cart class. I wanted to check for the presence of id in the array through array_key_exists in the method public function addItem ($ id) {$ id = (int) $ id; $ key = array_search ($ id, $ this-> items); if (! in_array ($ id, $ this-> items)) {array_push ($ this-> items, $ id); } Cookie :: set ('items', serialize ($ this-> items)); } Or is it wrong? - devmo

0