this is a code of a shopping cart i'm trying to make. the cart displays the products with: name, size, price, quantity and total. When checked out it saves on Mysql DB.
So it saves like this:
user_1 | pants | XL | 10 $ | 1 | 10 $ | user_1 | t-shirt | L | 5 $ | 2 | 10 $ | user_2 | sock s | 8 | 7 $ | 5 | 35 $ |
it's creating new row for each product for the same user. But what i need is this:
user_1 | pants, t-shirt | XL, L | 10, 5 $ | 1, 2 | 20 $ | user_2 | socks | 8 | 7 $ | 5 | 35 $ | user_3 | t-shirt, hat | M, S | 5, 10 $ | 1, 1 | 15 $ |
for the same user he implodes the data in the column of the same row.
code:
$products_array[$i] = array("user" => $user, "product" => $name_product, "size" => $size, "price" => $price, "quantity" => $each_item['quantity'], "total" => $cartTotal); if (isset($_SESSION["cart_array"]) && count($products_array) > 0 && isset($_POST['addOrder'])) { foreach ($products_array as $product) { $user = $_SESSION["user"]; $name_product = $product['name_product']; $size = $product['size']; $price = $product['price']; $quantity = $product['quantity']; $cartTotal = $product['total']; $result = mysqli_query($dbc,"INSERT INTO shop (user, product, size, price, quantity, total, date) VALUES('$user','$name_product','$size','$price','$quantity','$cartTotal',now())"); header("location: shop_cart.php"); } }
So i been googling arround, and found that what i need is a implode function. But my problem is where and how to set it right. I have tried some code already but it doesnt work. I dont want to flood the DB Table with multiple rows of the same purchase. Any other suggestions on what to do to achieve my goal is accepted.
Appreciate your help. Thank you.