Jump to content

implode and multidimensional array to mysql


Gladiador

Recommended Posts

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 |    | 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.

Edited by Gladiador
Link to comment
Share on other sites

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.

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.