Gladiador Posted December 11, 2013 Share Posted December 11, 2013 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. Link to comment https://forums.phpfreaks.com/topic/284721-implode-and-multidimensional-array-to-mysql/ Share on other sites More sharing options...
requinix Posted December 12, 2013 Share Posted December 12, 2013 Moved to MySQL because I dont want to flood the DB Table with multiple rows of the same purchase.Yes, actually, you do. Well, you might not want to, but that is what you should be doing. It is almost always bad to put multiple pieces of data into one column. Keep them as separate rows - you may think it's more effort now, but it will save you from problems later. Link to comment https://forums.phpfreaks.com/topic/284721-implode-and-multidimensional-array-to-mysql/#findComment-1462150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.