Hello. I'm asking for help in creating a very simple shopping cart. There is an output of goods from the database, how to make a basket of goods. Let’s say we add a “Add to cart” button to the products; when clicked, the product will be added to the cart. There is no need to save the cart in the database. Need a cart via cookies or session. The site itself: http://host1649597.hostland.pro/category/muzhskaya-parfyumeriya/ You need it to be as simple as possible, the main thing is that it works)

14 answers

Para bellum

The simplest thing:

class Cart ( /** * Cart data * * @var array */ protected $data = ; /** * Cart cookie name * * @var string */ protected $name = "cart"; public function __construct() ( $this->decode(); ) /** * Add product ID * * @param integer $id * @return void */ public function set($id) ( if (!in_array($id, $this->data )) ( $this->data = (int) $id; ) ) /** * Retrieving an array of product IDs * * @return array */ public function get() ( return $this->data; ) /** * Deleting an item from the cart * * @param integer $id * @return void */ public function delete($id) ( if (false !== $key = array_search($id, $this->data)) ( unset($ this->data[$key]); ) ) /** * Retrieving a cart from a cookie * * @return void */ protected function decode() ( $data = $_COOKIE[$this->name] ?? ""; if ($data = json_decode($data, true)) ( $this->data = array_filter($data, "is_int"); ) ) /** * Save the cart to a cookie * * @return void */ public function save () ( setcookie($this->name, json_encode($this->

Usage:

# Adding product ID to cart $cart = new Cart; $cart->set(1); $cart->save(); # Removing product ID from cart $cart = new Cart; $cart->delete(1); $cart->save(); # Getting product IDs from the cart $cart = new Cart; $ids = $cart->get();

A couple of clarifications: 1) If there are tools for working with request and response (Request and Response), then it is better not to access $_COOKIE directly and not to call setcookie, but to use those tools. 2) It will only work in PHP 7. For earlier versions write 66 like this:

$data = isset($_COOKIE[$this->name]) ? $_COOKIE[$this->name] : "";

M_Sergey

I won’t write codes, but the principle should be as follows. The "Add to Cart" button should be either a form button or a link image, depending on the method used to transfer data to the server POST or GET. This data should contain the ID of the product and, for example, its quantity, i.e. two meanings. On the server, the script processes these two values ​​and places them in the SESSION superglobal array. And from it you get information about the added products in the cart itself. Based on the available data in the SESSION array, you can style the “Cart” button (highlight it there and write how many products are in it...)

Well here is the button code

echo "

NameCodePriceBranddescription
". $row["name"]."". $row["code"]."". $row["price"]."". $row["brand"]."". $row["description"]."Add

";

namely

Add

Para bellum

Something is difficult I can’t understand The code that I provided, save it in separate file, then connect it where you need to use the cart. How to use, I said after the signature "Usage:". Nothing complicated. Adding to cart is where I am powerless. And for whom did I write the ready-made cart code? For example, we saved that code in the file "Cart.php". Then, in add_to_trash.php (better in add_to_cart.php) we write:

Cart .korzina ( margin-left: 200px; background: black; width: 200px; height: 200px; color: white; overflow: scroll; ) SAMSUNG(250$) NOKIA(150$) APPLE(300$) MOTOROLA(400$ ) TESLA(500$)

here is my version))) Para bellum is more correct! - mine is just for testing))

look at what version of php you have in the new one - you can create an array $a = ; and the old one $a = array();

Para bellum

look at what version of php you have. It’s probably still 5.3. Parse error: syntax error, unexpected "[" in cart.php on line 9 If you need it to work in older versions, here it is:


Close